double精确位数摘记
double精确位数摘录
突然需要对double的数字做精确记录,go之发现网上给出了很好的答案,故摘录在此,谢谢网上的各处答疑。
出自如下2处 :
1: http://topic.****.net/t/20030318/12/1544852.html
2: http://bbs.chinaunix.net/viewthread.php?tid=723528
一:利用NumberFormat
import java.text.*; public class Test{ public static void main(String[]args){ float value=1.23456789f; NumberFormat numFormat = NumberFormat.getNumberInstance(); numFormat.setMaximumFractionDigits(2); String str = numFormat.format(value); System.out.println(str); } }
说明下,默认的是会做四舍五入了。例如1.236 取2位得到的是 1.24 ,如果取0 则默认是取整操作了,另外传递负数也是取整,因为max(0,value)
二:利用BigDecimal
import java.math.BigDecimal; String str = String.valueOf((new BigDecimal(1.234567)).setScale(2,BigDecimal.ROUND_HALF_UP));
BigDecimal提供了如下几种round modes
// Rounding Modes /** * Rounding mode to round away from zero. Always increments the * digit prior to a nonzero discarded fraction. Note that this rounding * mode never decreases the magnitude of the calculated value. */ public final static int ROUND_UP = 0; /** * Rounding mode to round towards zero. Never increments the digit * prior to a discarded fraction (i.e., truncates). Note that this * rounding mode never increases the magnitude of the calculated value. */ public final static int ROUND_DOWN = 1; /** * Rounding mode to round towards positive infinity. If the * {@code BigDecimal} is positive, behaves as for * {@code ROUND_UP}; if negative, behaves as for * {@code ROUND_DOWN}. Note that this rounding mode never * decreases the calculated value. */ public final static int ROUND_CEILING = 2; /** * Rounding mode to round towards negative infinity. If the * {@code BigDecimal} is positive, behave as for * {@code ROUND_DOWN}; if negative, behave as for * {@code ROUND_UP}. Note that this rounding mode never * increases the calculated value. */ public final static int ROUND_FLOOR = 3; /** * Rounding mode to round towards {@literal "nearest neighbor"} * unless both neighbors are equidistant, in which case round up. * Behaves as for {@code ROUND_UP} if the discarded fraction is * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}. Note * that this is the rounding mode that most of us were taught in * grade school. */ public final static int ROUND_HALF_UP = 4; /** * Rounding mode to round towards {@literal "nearest neighbor"} * unless both neighbors are equidistant, in which case round * down. Behaves as for {@code ROUND_UP} if the discarded * fraction is {@literal >} 0.5; otherwise, behaves as for * {@code ROUND_DOWN}. */ public final static int ROUND_HALF_DOWN = 5; /** * Rounding mode to round towards the {@literal "nearest neighbor"} * unless both neighbors are equidistant, in which case, round * towards the even neighbor. Behaves as for * {@code ROUND_HALF_UP} if the digit to the left of the * discarded fraction is odd; behaves as for * {@code ROUND_HALF_DOWN} if it's even. Note that this is the * rounding mode that minimizes cumulative error when applied * repeatedly over a sequence of calculations. */ public final static int ROUND_HALF_EVEN = 6; /** * Rounding mode to assert that the requested operation has an exact * result, hence no rounding is necessary. If this rounding mode is * specified on an operation that yields an inexact result, an * {@code ArithmeticException} is thrown. */ public final static int ROUND_UNNECESSARY = 7;
三 :利用DecimalFormat
通过使用java.text package包中提供的类型,将数字类型装换成指定的格式。 http://www.javaalmanac.com/egs/index.html // The 0 symbol shows a digit or 0 if no digit present NumberFormat formatter = new DecimalFormat("000000"); String s = formatter.format(-1234.567); // -001235 // notice that the number was rounded up // The # symbol shows a digit or nothing if no digit present formatter = new DecimalFormat("##"); s = formatter.format(-1234.567); // -1235 s = formatter.format(0); // 0 formatter = new DecimalFormat("##00"); s = formatter.format(0); // 00 // The . symbol indicates the decimal point formatter = new DecimalFormat(".00"); s = formatter.format(-.567); // -.57 formatter = new DecimalFormat("0.00"); s = formatter.format(-.567); // -0.57 formatter = new DecimalFormat("#.#"); s = formatter.format(-1234.567); // -1234.6 formatter = new DecimalFormat("#.######"); s = formatter.format(-1234.567); // -1234.567 formatter = new DecimalFormat(".######"); s = formatter.format(-1234.567); // -1234.567 formatter = new DecimalFormat("#.000000"); s = formatter.format(-1234.567); // -1234.567000 // The , symbol is used to group numbers formatter = new DecimalFormat("#,###,###"); s = formatter.format(-1234.567); // -1,235 s = formatter.format(-1234567.890); // -1,234,568 // The ; symbol is used to specify an alternate pattern for negative values formatter = new DecimalFormat("#;(#)"); s = formatter.format(-1234.567); // (1235) // The ' symbol is used to quote literal symbols formatter = new DecimalFormat("'#'#"); s = formatter.format(-1234.567); // -#1235 formatter = new DecimalFormat("'abc'#"); s = formatter.format(-1234.567); // -abc1235
再次对网上各位答仁表示感谢。