Possible Duplicate:
Rounding in java Float.parseFloat
I want to convert strings to float i say:
System.out.println(Float.parseFloat("1553781.9829"));
Output:
1553782.0
I say:
System.out.println(Float.valueOf("1553781.9829"));
Output:
1553782.0
How to get Float without lossing precision?
use System.out.println(Double.parseDouble("1553781.9829"));
instead
float has a limitation of smaller size (4 bytes) so it's not good for big decimals, double has the double size (8 bytes)
If you are looking for accuracy, I would suggest BigDecimal
This is just like normal wrapper class which provides methods for all your operations. And yes it takes argument as String as well.
BigDecimal bd = new BigDecimal("1553781.9829");
System.out.println(" bd :"+ bd); //prints the same value
URL : http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With