float in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

float in java

I am facing a very weird issue in this simple addition code. The answer of (1.1)+(-1) is coming 0.100000024 https://code.sololearn.com/cihF01qib8v3/?ref=app

29th Dec 2018, 2:41 PM
harshit
harshit - avatar
1 Answer
+ 4
The reason is float type in Java is not so accurate 32- bit. Instead of float type you can use double or even BigDecimal - it depends on accuracy you need. You can read more about it here: https://stackoverflow.com/questions/27598078/float-and-double-datatype-in-java. Hope that explanation can help you.
29th Dec 2018, 2:45 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar