2010-11-25 回答
import decimal
a = '12.11111'
decimal.Decimal(a).quantize(decimal.Decimal('0.01'))
Decimal('12.11')
补充:
>>> import decimal>>> a = 1>>> b = decimal.Decimal(a).quantize(decimal.Decimal('0.01'))>>> bDecimal('1.00')>>> print b1.00
追问:
这是我做的程序:(要求显示找多少元,多少角,多少分!!)
#=======================================================import
from math import*
#======================================================main module
def main():
price,payment=dataInput()
change=calculateChange(price,payment)
printResults(price,payment,change)
#======================================================= input
def dataInput():
again=0
while again==0:
try:
price=float(input('Price of purchase is '))
payment=float(input('How much you pay? '))
if payment>price:
again=1
else:
print('The payment must greater then the price')
except(ValueError):
print('Bad purchase number!')
return price,payment
#=========================================================== calculate price
def calculateChange(price,payment):
change=payment-price
return change
def printResults(price,payment,change):
import decimal
a=decimal.Decimal(price).quantize(decimal.Decimal('0.01'))
b=decimal.Decimal(payment).quantize(decimal.Decimal('0.01'))
c=decimal.Decimal(change).quantize(decimal.Decimal('0.01'))
print(('Amount of purchase: $'),a)
print(('Payment: $'),b)
print(('Amount of change: $'),c)
m=str(c)
print(' ',m[0],'dollors')
print(' ',m[2],'dimes')
print(' ',m[3],'pennys')
return
#=========================================================== call
main()
现在就是printResult这一部分不会编,我输入价格是4.5,然后支付10,找5.5。然后没有分,就产生错误,请问这怎么修正,才能显示5元,5角,和0分呢?
(加到100分!!!!!)
追答:
#! usr/bin/env python#coding:utf-8
from decimal import Decimal
#======================================================= inputdef dataInput(): again=0 while again==0: try: price = raw_input('Price of purchase is ') payment = raw_input('How much you pay? ') price=Decimal(price.strip()).quantize(Decimal('0.01')) payment=Decimal(payment.strip()).quantize(Decimal('0.01')) if payment>price: again=1 else: print 'The payment must greater then the price' except Exception, e: print e print 'Bad purchase number!' return price,payment#=========================================================== calculate pricedef calculateChange(price,payment): change=payment-price return change#========================================================= print
def convert(money): if not isinstance(money, Decimal): return 'error' money = str(money) yuan, jiao_fen = money.split('.') jiao, fen = jiao_fen[0], jiao_fen[1] return '找零: %s元%s角%s分' % (yuan, jiao, fen) def printResults(): price, payment = dataInput() change = calculateChange(price, payment) print 'Amount of purchase: %s' % price print 'Payment: %s' % payment print 'Amount of change: %s' % change print convert(change) #======================================================main module
if __name__ == '__main__': printResults()说明:
首先你用input()进行输入,如果是输入4.5的话,就直接是float的型了。而Decimal转换float的时候,要先把其转成字符串形式,所有我改成了raw_input()(当成字符串输入)了。其次,算找零的时候不能这样直接用切片操作。如果是4.5 我给了100块钱呢?那元就不是只有一位了。所有先split('.')再切片。位置就固定了。一般用main方法的时候用下面的方法:if __name__ == '__main__': main()
结果:Price of purchase is 4.5How much you pay? 4.6Amount of purchase: 4.50Payment: 4.60Amount of change: 0.10找零: 0元1角0分============Price of purchase is 4.5How much you pay? 100Amount of purchase: 4.50Payment: 100.00Amount of change: 95.50找零: 95元5角0分
补充:
怎么格式全乱了- -#! usr/bin/env python#coding:utf-8
from decimal import Decimal
#======================================================= inputdef dataInput(): again=0 while again==0: try: price = raw_input('Price of purchase is ') payment = raw_input('How much you pay? ') price=Decimal(price.strip()).quantize(Decimal('0.01')) payment=Decimal(payment.strip()).quantize(Decimal('0.01')) if payment>price: again=1 else: print 'The payment must greater then the price' except Exception, e: print e print 'Bad purchase number!' return price,payment#=========================================================== calculate pricedef calculateChange(price,payment): change=payment-price return change#========================================================= print
def convert(money): if not isinstance(money, Decimal): return 'error' money = str(money) yuan, jiao_fen = money.split('.') jiao, fen = jiao_fen[0], jiao_fen[1] return '找零: %s元%s角%s分' % (yuan, jiao, fen) def printResults(): price, payment = dataInput() change = calculateChange(price, payment) print 'Amount of purchase: %s' % price print 'Payment: %s' % payment print 'Amount of change: %s' % change print convert(change) #======================================================main module
if __name__ == '__main__': printResults()
追问:
money 是怎么来的啊?
追答:
money 是个形参。传 change过去用的
convert(change)
追问:
+我Q吧:308216226