system out output for double numbers in a java program

Posted by Nikunj Chauhan on Stack Overflow See other posts from Stack Overflow or by Nikunj Chauhan
Published on 2012-03-22T11:25:56Z Indexed on 2012/03/22 11:29 UTC
Read the original article Hit count: 278

Filed under:
|

I have a program where I am generating two double numbers by adding several input prices from a file based on a condition.

String str;
double one = 0.00;
double two = 0.00;
BufferedReader in = new BufferedReader(new FileReader(myFile));

while((str = in.readLine()) != null){
     if(str.charAt(21) == '1'){
         one += Double.parseDouble(str.substring(38, 49) + "." + str.substring(49, 51));
     }

     else{
         two += Double.parseDouble(str.substring(38, 49) + "." + str.substring(49, 51));
     }
   }

in.close();
System.out.println("One: " + one);
System.out.println("Two: " + two);      

The output is like:

One: 2773554.02
Two: 6.302505836000001E7

Question:

None of the input have more then two decimals in them. The way one and two are getting calculated exactly same.

Then why the output format is like this.

What I am expecting is:

One: 2773554.02
Two: 63025058.36

Why the printing is in two different formats ? I want to write the outputs again to a file and thus there must be only two digits after decimal.

© Stack Overflow or respective owner

Related posts about java

Related posts about string