Priyanshu
Priyanshu

Reputation: 13

discrepancy in double to Float class conversion in java

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));
}

Upvotes: 1

Views: 54

Answers (1)

Simon Byrne
Simon Byrne

Reputation: 7874

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).

Upvotes: 1

Related Questions