RamRam RamRam - 2 years ago 83
Java Question

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?

Answer Source

Try double instead.

double abc = (double) (5042034.0 + 1425837.2);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download