python怎么保留两位小数是多少_python 怎么保留2位小数

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

#========================================================= print

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值