Python Forum

Full Version: Why is this multiplication not working?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,I'm very new to Python, and I'm having trouble with some basic math.  I've getting a user to input a number and assigning it to a variable.  Then I'm multiplying it by the value of another variable, which I've set to 0.1 and displaying the result.  When the input is 3, and I multiply it by the other variable, instead of getting .3, I'm getting .3000000000004.

What could cause the multiplication to return what appears to be an incorrect value?
Show the code, it's impossible to answer correctly without.
testMultiplier=0.1
testInput=int(input("Enter a number"))
testOutput=testInput*testMultiplier
print (testOutput)


If I input 3 when asked, it spits out 3.000004
Quote:instead of getting .3, I'm getting .3000000000004.
It's the way floating point arithmetic work,Basic Answers Python doc.
There is a Decimal module.
>>> 0.4 - 0.1
0.30000000000000004

>>> from decimal import Decimal
>>> Decimal('0.4') - Decimal('0.1')
Decimal('0.3')
thanks, that was very helpful.