5

I was analyzing case where DecimalFormat rounded one BigDecimal number and on other machine, it is truncated.

I have verified all configurations on both machines (and all are same, i assume). Only difference which i have figured out is JDK version.

Machine 1 is running on JDK1.6 . But, i have tried same with JDK1.7 on Machine 1, it is working same as with JDK1.6.

Machine 2 is running on JDK1.7

Following is the code snippet:

DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
BigDecimal anObject = new BigDecimal("3.8880");
String str = decimalFormat.format(((Number)anObject).doubleValue());
System.out.println(str);

On Machine 1 Result is : 3.39

On Machine 2 result is : 3.38

Rohit Batta
  • 482
  • 4
  • 16
  • 2
    I don't see how the number 3.8880 could be rounded or truncated to 3.38 or 3.39. Do you mean 3.88/3.89? Or didn't you do the test correctly? – JB Nizet Jun 06 '16 at 08:01
  • Try to run on command prompt "java -version". Maybe just a JVM bug. – Aris2World Jun 06 '16 at 08:04

1 Answers1

6

There was a bug introduced in JDK7 on DecimalFormat. See this question for more information: Is inconsistency in rounding between Java 7 and Java 8 a bug?

Community
  • 1
  • 1
Arnaud Develay
  • 3,920
  • 2
  • 15
  • 27
  • Thanks Arnaud, I have checked it on oracle site too and found same bug reported there also. Check this reference [Bug on Oracle](https://bugs.openjdk.java.net/browse/JDK-8039915) site – Rohit Batta Jun 07 '16 at 05:38