如何在python中打印十进制值

Mik*_*yce 4 python decimal floating-accuracy

print("enter start() to start the program")

def start():
    print("This script converts GBP into any currency based on the exchange rate...")
    print(" ") #enters a line
    exchangeRate = int(input("Enter the exchange rate (Eg: 0.80)"))
    print("how much would you like to convert???")                       
    gpb = int(input())
    print(gpb*exchangeRate)
Run Code Online (Sandbox Code Playgroud)

如果我将汇率设为0.81而我输入£1则总是返回0.

Lev*_*von 7

使用float(),而不是int()用你的input()电话.也就是说,

    gpb = float(input())
Run Code Online (Sandbox Code Playgroud)

否则,如果用户输入0.81,int()0在转换期间将其截断为.

通过使用float()你将保持提供的十进制值作为输入,你的计算应该产生你期望的结果.