The code is :
public void testDoubleToBigDecimalViaString(){ double d = 0.1; double e = 0.11; double f = e - d; // MathContext mc1 = MathContext.DECIMAL32; // MathContext mc2 = MathContext.UNLIMITED; MathContext mc1 = new MathContext(7); MathContext mc2 = new MathContext(34); BigDecimal bd1 = new BigDecimal(d); BigDecimal bd2 = new BigDecimal(String.valueOf(d)); System.out.println("bd1 = " + bd1.toString()); System.out.println("bd2 = " + bd2.toString()); BigDecimal bd9 = new BigDecimal(d, mc1); BigDecimal bd10 = new BigDecimal(String.valueOf(d), mc1); System.out.println("bd9 = " + bd9.toString()); System.out.println("bd10 = " + bd10.toString()); BigDecimal bd11 = new BigDecimal(d, mc2); BigDecimal bd12 = new BigDecimal(String.valueOf(d), mc2); System.out.println("bd11 = " + bd11.toString()); System.out.println("bd12 = " + bd12.toString()); BigDecimal bd3 = new BigDecimal(f); BigDecimal bd4 = new BigDecimal(String.valueOf(f)); System.out.println("bd3 = " + bd3.toString()); System.out.println("bd4 = " + bd4.toString()); BigDecimal bd5 = new BigDecimal(f, mc1); BigDecimal bd6 = new BigDecimal(String.valueOf(f), mc1); System.out.println("bd5 = " + bd5.toString()); System.out.println("bd6 = " + bd6.toString()); BigDecimal bd7 = new BigDecimal(f, mc2); BigDecimal bd8 = new BigDecimal(String.valueOf(f), mc2); System.out.println("bd7 = " + bd7.toString()); System.out.println("bd8 = " + bd8.toString()); }
The result with Sun JDK 1.6 is :
bd1 = 0.1000000000000000055511151231257827021181583404541015625
bd2 = 0.1
bd9 = 0.1000000
bd10 = 0.1
bd11 = 0.1000000000000000055511151231257827
bd12 = 0.1
bd3 = 0.00999999999999999500399638918679556809365749359130859375
bd4 = 0.009999999999999995
bd5 = 0.01000000
bd6 = 0.01000000
bd7 = 0.009999999999999995003996389186795568
bd8 = 0.009999999999999995
0 comments:
Post a Comment