我正在尝试建立一个程序,要求你的钱,并转换成一元,25分,1角,5分,和便士。
originalvalue = float(input("How much money do you have?\n"))
dollarvalue = int(originalvalue) #This is the amount of single dollars they have
coinvalue = originalvalue - dollarvalue #This is the amount of coins that they have
print(coinvalue, "1")
它将返回以下内容:
How much money do you have?
5.56
0.5599999999999996 1```
Shouldn't this be subtracting 5 from 5.56 which should be 0.56.
Is there a way to make this float stop terminating?
这是在Python中表示十进制值的一个怪异之处。 如果你想让它非常精确的话。 使用decimal.decimal
from decimal import Decimal
originalvalue = Decimal(input("How much money do you have?\n"))
dollarvalue = int(originalvalue) #This is the amount of single dollars they have
coinvalue = originalvalue - dollarvalue #This is the amount of coins that they have
print(coinvalue, "1")