Reputation: 11
I'm using GWT 2.0 and when I try to use NumberFormat to format a Double the results are not as expected:
NumberFormat format = NumberFormat.getFormat( "#.########" );
Double d = new Double("0.256281093911");
format.format(d);
formatted string: 0.02147484
As you can see the formatted value is wrong (this can be seen in the gwt showcase). Is this something related to the custom format I'm using (#.########)? or is this a bug in the GWT formatter? If this is a bug, have someone found a workaround? Thanks.
Upvotes: 1
Views: 4515
Reputation: 21586
A Double is stored in binary format in java in general. Working with binary float values is in most cases not what you want - conversion between binary and decimal floats causes rounding errors.
Try "BigDecimal", which stores float values in DECIMAL format, not in binary format.
Sample code:
NumberFormat format = NumberFormat.getFormat("#.########");
format.format(new BigDecimal("0.256281093911"));
Upvotes: 1
Reputation: 1577
link text you might try NumberFormat.getDecimalFormat. Also, it might have something to do with your browser's locale and/or it's decimal delimiter.
Upvotes: 1