Search code examples
javanumbersnumber-formattingbigdecimaldecimalformat

DecimalFormat results in two different results on different machines


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


Solution

  • 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?