I am trying the below code but after executing 344741.79 value is showing as 344741.78
Means after decimal place .79 is getting converted to .78, I am really wondering why I am not getting the exactly same value after conversion and which logic is doing this?
public static void main(String[] s) {
double a = 344741.79;
System.out.print(new Float(a));
}
aUserHimself
1,5972 gold badges18 silver badges27 bronze badges
1 Answer 1
Float is only guaranteed to accurately store 6 significant decimal digits. In the case of 344741.79, the 2 nearest Floats are 344741.78125 (which is printed as 344741.78) and 344741.8125 (which is printed as 344741.8).
answered Mar 17, 2017 at 8:41
Simon Byrne
7,9642 gold badges32 silver badges51 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Priyanshu
Thank you for your answer and time :)
Simon Byrne
If you're happy with the answer, please accept it by clicking the tick.
Explore related questions
See similar questions with these tags.
lang-java
doubleto afloatin Java.double(which cannot represent all decimal fractions either).