Search code examples
javafloating-pointprecisionieee-754

Issue with adding Float values In java


I have two values

5042034.0 
1425837.2

while I am adding like below

float abc = (float) (5042034.0 + 1425837.2);

I am expecting a result of 6467871.2

But I am getting 6467871.0

How could I get the 6467871.2 with the help of float?

I tried with

 float c = (float) (1.1 + 2.2) ;

I got a result : 3.3

What is the reason behind this?


Solution

  • Try double instead.

    double abc = (double) (5042034.0 + 1425837.2);