float to double conversion mistake??
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Norbert_Nagy
Posted On:   Monday, June 11, 2001 01:19 PM

Does anybody know why the following program gives the output 123.45600128173828 instead of 123.456?


public class Test {

public static void main(String [] args) {

float f = 123.456f;

double d = f;

System.out.println(d);

}

}

Re: float to double conversion mistake??

Posted By:   Michael_Wax  
Posted On:   Monday, June 11, 2001 01:44 PM

This is not a mistake. Use of float and double does not guarantee accuracy to the number of decimal places you seek, as the internal representation of your number will likely not correspond precisely to the number you entered. If you need to maintain your number as 123.456, then you should use the BigDecimal class.
About | Sitemap | Contact