【问题标题】:how to print decimal values in python如何在python中打印十进制值
【发布时间】:2012-09-13 17:35:20
【问题描述】:
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)

如果我将汇率设为 0.81 并输入 1 英镑,它总是返回 0。

【问题讨论】:

    标签: python decimal floating-accuracy


    【解决方案1】:

    在您的input() 呼叫中使用float() 而不是int()。即,

        gpb = float(input())
    

    否则如果用户输入0.81int() 将在转换过程中将其截断为0

    通过使用float(),您将保留作为输入提供的十进制值,并且您的计算应该会产生您期望的结果。

    【讨论】:

      【解决方案2】:

      您将类型指定为 int .....当您将 1 乘以 0.81 时,这些是整数 (0,1,2,3,4,5,6,7,8....),您得到 0.81。 ....整数的键号是点之前的数字,在这种情况下为零。所以就像前面的回答一样,只是简单地说改变变量的类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-19
        • 2013-03-31
        • 2017-06-22
        • 1970-01-01
        • 2012-09-01
        • 2016-12-13
        • 2012-10-09
        • 1970-01-01
        相关资源
        最近更新 更多