+ 2

Wrong calculator?

Why this code outputs 5.3500000000000005 instead of 5.35? print(5.00 * 1.07) https://code.sololearn.com/cXBn8i7yNh3p/?ref=app

15th Aug 2022, 3:00 PM
Amirreza
5 Respostas
+ 4
https://www.sololearn.com/discuss/3007535/?ref=app https://www.sololearn.com/Discuss/1477626/?ref=app
15th Aug 2022, 3:08 PM
Jayakrishna 🇮🇳
+ 3
Yes. Use round method print(round(5.00 * 1.07, 2) )
15th Aug 2022, 3:27 PM
Jayakrishna 🇮🇳
+ 2
# Hi, Amirreza Amiri ! # If you want to work with decimal numbers, and it is important for # you to get correct decimal answers, then you can use Decimal from the # decimal module: from decimal import Decimal x = Decimal('5') y = Decimal('1.07') print(x*y) # gives: 5.35 # N.B. The numbers are in quotes (are strings), like ”1.07” .
15th Aug 2022, 7:03 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Jayakrishna🇮🇳 Thanks a lot. Is there any way to get the 5.35 output? I only need two decimals.
15th Aug 2022, 3:23 PM
Amirreza
+ 1
Thank you Per Bratthammar 🙏 It was a great method 👌
16th Aug 2022, 5:52 PM
Amirreza