Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some floating point numbers appear with a trailing 0

Tags:

Does anyone know why the numbers 0.001 to 0.009 are rendered to a String with a trailing 0 but other numbers do not. e.g. numbers 0.01 to 0.09 do not.

System.out.println(Locale.getDefault());
for (int i = 0; i <= 20; i++)
    System.out.println(i / 1e3);

prints

en_GB
0.0
0.0010
0.0020
0.0030
0.0040
0.0050
0.0060
0.0070
0.0080
0.0090
0.01
0.011
0.012
0.013
0.014
0.015
0.016
0.017
0.018
0.019
0.02

EDIT The code for DecimalFormat doesn't appear to be locale dependant. If I run

for (Locale l : Locale.getAvailableLocales())   {
    Locale.setDefault(l);
    System.out.println(l + " " + 1 / 1e3);
}

on Java 6 update 26 on Ubuntu 11.04 I get

ja_JP 0.0010
es_PE 0.0010
en 0.0010
... many locales with the same result ...
sv_SE 0.0010
da_DK 0.0010
es_HN 0.0010

on Java 7 on same system I get

ms_MY 0.001
ar_QA 0.001
is_IS 0.001
... many locales with the same result ...
el_CY 0.001
hu 0.001
fr_FR 0.001
like image 819
Peter Lawrey Avatar asked Sep 27 '11 05:09

Peter Lawrey


2 Answers

This was identified as a bug in Java 1.3 - Java 6: http://bugs.java.com/view_bug.do?bug_id=4428022‎

EDIT: As to why this happens, here's the fix referred to in the bug report that was ported from OpenJDK 6: http://hg.openjdk.java.net/jdk6/jdk6/jdk/rev/8159687b6316

Turns out it's an off-by-one error. (The fix changes <= to <).

like image 51
avh4 Avatar answered Oct 29 '22 16:10

avh4


For those interested, here is a diff between the FloatingDecimal class responsible for creating the string representation of the double. As you can see from the diff, the patch fixes the special case encountered when the exponent is -3 in the dtoa() method.

like image 27
Sanjay T. Sharma Avatar answered Oct 29 '22 18:10

Sanjay T. Sharma