Alright, so I got a test program:
public class ParseTest {
public static void main(String[] args){
String s = "-0.000051";
System.out.println(s + " != " + Float.parseFloat(s));
}
}
and it’s not out putting the same number on both sides. This is the output:
-0.000051 != -5.1E-5
So, why isn’t it outputting the same number on both sides?
It is the same number just different scientific representation,
For example:
and
How to get same representation ?
If you want the same representation then use
BigDecimalWho is converting it to this format ?
when you convert primitive to String by for example
It calls the
Float.valueOf(f)which inturns calls theFloat.toString()which inturn calls thenew FloatingDecimal(f).toJavaFormatString();this is where this stuff is encapsulated