Jakob Jakob - 26 days ago 21
Java Question

Investigating a Java bug regarding String.valueOf(float)

In Java is it a possibility that String.valueOf(float) would format a float number differently based on what operating system the code is run on, the version of java and/or the operating systems locale.

For example, with the float number 4.5 would it ever be formatted to "4,5" instead of "4.5"?

dit dit
Answer

String.valueOf(float) calls Float.toString().

Float.toString() calls intern sun.misc.FloatingDecimal.toJavaFormatString(float)

The result string will never contain the sign , bacause of hard-coded '.' (ASCII: 46) inside the BinaryToASCIIBuffer.getChars(chars[])

You can see it if you decompile sun.misc.FloatingDecimal class (in my case java 8 jdk) or see the (similar) implementation in openjdk.