Difficulties on opertions with variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difficulties on opertions with variables

why haven't we got the good result of 49.5 and why. 49.50000000001 ? https://code.sololearn.com/cDgIcb6DegBY/?ref=app

26th Oct 2020, 11:25 AM
Theo Martier
Theo Martier - avatar
5 Answers
+ 3
You can use printf() method for formatting floating numbers. System.out.printf("%.2f",var2);
26th Oct 2020, 11:35 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
floats and double cannot be precise, because of their binary representation. It often leads to this type of errors, due to floating point precision.
26th Oct 2020, 11:30 AM
Théophile
Théophile - avatar
+ 1
It's due to Rounding Error.
26th Oct 2020, 11:33 AM
Soheil
Soheil - avatar
+ 1
1.1 can't be represented as a binary decimal number.
26th Oct 2020, 11:36 AM
Seb TheS
Seb TheS - avatar
+ 1
Thank you Theophile, Soheil, Magat Sam and Seb TheS!!!
26th Oct 2020, 12:10 PM
Theo Martier
Theo Martier - avatar