显示小数位(Python 2.7和Decimal数据类型)

时间:2013-12-05 10:52:25

标签: python-2.7 decimal

我正在尝试编写货币转换器程序,而不是使用浮点数,而不是尝试使用十进制数据类型在Python 2.7.5中编写程序。

程序工作并显示结果,但是当使用getcontext()。prec = 2时,这会将显示的数字量限制为2,而不是小数位数

代码如下所示:

#setup the decimal data type (including number of decimal places)
import decimal
decimal.getcontext().prec = 2

#get the current value and type from the user
currencyAmount = decimal.Decimal(raw_input('please enter the amount: '))
currencyType = int(raw_input('please enter the type (1 = pound, 2 = euro, 3 = dollar, 4 =      yen): '))

#set the exchange rates (based on sterling)
euro = decimal.Decimal('1.5')
dollar = decimal.Decimal('2.7')
yen = decimal.Decimal('30.8')

#convert the currency into pound sterling
if (currencyType == 2):
    currencyAmount = currencyAmount / euro
elif (currencyType == 3):
    currencyAmount = currencyAmount / dollar
elif (currencyType == 4):
    currencyAmount = currencyAmount / yen

#ask the user what currency they want it converted into
currencyConvert = int(raw_input('please enter the currency you would like to convert to (1 = pound, 2 = euro, 3 = dollar, 4 = yen): '))

#convert the currency into the new format (pound already done in previous steps)
if (currencyConvert == 2):
    currencyAmount = currencyAmount * euro
elif (currencyConvert == 3):
    currencyAmount = currencyAmount * dollar
elif (currencyConvert == 4):
    currencyAmount = currencyAmount * yen

#display the result to the user
print 'the result of the currency conversion was ',  currencyAmount

更新:

尝试了建议的代码,但在运行程序时遇到了以下错误:

Traceback (most recent call last):
    File "C:\Users\nhornus154\Desktop\currencyConverter.py", line 34, in <module>
    print 'the result of the currency conversion was ', currencyAmount.quantize(Decimal('0.00'))
NameError: name 'Decimal' is not defined

也尝试更改为decimal.Decimal,但是回复说参数对于量化

是不正确的

1 个答案:

答案 0 :(得分:0)

精度遵循sig figs,而不是小数位。 你应该删除getcontext().prec = 2 并将最后一行更改为:

print 'the result of the currency conversion was ',  currencyAmount.quantize(Decimal('0.00'))

修改

我在下面尝试过这段代码并且工作正常:

#setup the decimal data type (including number of decimal places)
import decimal

#get the current value and type from the user
currencyAmount = decimal.Decimal(raw_input('please enter the amount: '))
currencyType = int(raw_input('please enter the type (1 = pound, 2 = euro, 3 = dollar, 4 =      yen): '))

#set the exchange rates (based on sterling)
euro = decimal.Decimal('1.5')
dollar = decimal.Decimal('2.7')
yen = decimal.Decimal('30.8')

#convert the currency into pound sterling
if (currencyType == 2):
    currencyAmount = currencyAmount / euro
elif (currencyType == 3):
    currencyAmount = currencyAmount / dollar
elif (currencyType == 4):
    currencyAmount = currencyAmount / yen

#ask the user what currency they want it converted into
currencyConvert = int(raw_input('please enter the currency you would like to convert to (1 = pound, 2 = euro, 3 = dollar, 4 = yen): '))

#convert the currency into the new format (pound already done in previous steps)
if (currencyConvert == 2):
    currencyAmount = currencyAmount * euro
elif (currencyConvert == 3):
    currencyAmount = currencyAmount * dollar
elif (currencyConvert == 4):
    currencyAmount = currencyAmount * yen

#display the result to the user
print 'the result of the currency conversion was ',  currencyAmount.quantize(decimal.Decimal('0.00'))

输出:

please enter the amount: 100
please enter the type (1 = pound, 2 = euro, 3 = dollar, 4 =      yen): 2
please enter the currency you would like to convert to (1 = pound, 2 = euro, 3 = dollar, 4 = yen): 4
the result of the currency conversion was  2053.33