0

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?

CyanPrime
  • 5,096
  • 12
  • 58
  • 79
  • 1
    It *is* the same number, dude. One is decimal, the other is exponential: both notations show the same value! Your "println()" is implicitly doing "Float.toString()". If you want a different notation, look at "String.format()". EX: String.format("%.2f", floatValue); – paulsm4 Jun 09 '12 at 19:26

1 Answers1

2

It is the same number just different scientific representation,

For example:

100 = 1E2 

and

0.1 = 1E-1

How to get same representation ?

If you want the same representation then use BigDecimal

System.out.println(s + " != " + new BigDecimal(s));

Who is converting it to this format ?

when you convert primitive to String by for example

float f = 100.123;
String result = "" + f; 

It calls the Float.valueOf(f) which inturns calls the Float.toString() which inturn calls the new FloatingDecimal(f).toJavaFormatString(); this is where this stuff is encapsulated

jmj
  • 237,923
  • 42
  • 401
  • 438