003_013 Python 将数decimal用于货币处理

代码如下:

#encoding=utf-8

print '中国'

#将数用于货币处理

import decimal

def italformat(value, places=2, curr='EUR',sep='.',dp=',',pos='',neg='-', overall=10):
    """ 将十进制value转化为财务格式的字符串  
    places:  十进制小数点后面的数字的位数  
    curr:    可选的货币符号(可能为空)  
    sep:     可选的分组(三个一组)分隔符(逗号、句号或空白)  
    dp:     小数点指示符(逗号或句号);当places是0时  
           小数点被指定为空白  
    pos:     正数的可选的符号:"+",空格或空白  
    neg:     正数的可选的符号:"-"、"(",空格或空白  
    overall:  最终结果的可选的总长度,若长度不够,左边货币符号  
           和数字之间会被填充符占据  
    """
    q=decimal.Decimal((0, (1,), -places))    # 2 places --> '0.01'    
    sign, digits, exp = value.quantize(q).as_tuple( )  
    result = []  
    digits = map(str, digits)  
    append, next = result.append, digits.pop  
    for i in range(places):  
      if digits:  
        append(next( ))  
      else:  
        append('0')  
    append(dp)  
    i = 0 
    while digits:  
      append(next( ))  
      i += 1  
      if i == 3 and digits:  
        i = 0 
        append(sep)  
    while len(result) < overall: 
      append(' ')  
    append(curr)  
    if sign: append(neg)  
    else: append(pos)  
    result.reverse( )  
    return ''.join(result)  
# 获得计算用的小计  
def getsubtotal(subtin=None):  
    if subtin == None:  
      subtin = input("Enter the subtotal: ")  
    subtotal = decimal.Decimal(str(subtin))  
    print "\n subtotal:         ",italformat(subtotal)  
    return subtotal  
# 指定意大利税法函数  
def cnpcalc(subtotal):  
    contrib = subtotal * decimal.Decimal('.02')  
    print "+ contributo integrativo 2%:    ",italformat(contrib, curr='')  
    return contrib  
def vatcalc(subtotal, cnp):  
    vat = (subtotal+cnp) * decimal.Decimal('.20')  
    print "+ IVA 20%:            ", italformat(vat, curr='')  
    return vat  
def ritacalc(subtotal):  
    rit = subtotal * decimal.Decimal('.20')  
    print "-Ritenuta d'acconto 20%:    ", italformat(rit, curr='')  
    return rit  
def dototal(subtotal, cnp, iva=0, rit=0):  
    totl = (subtotal+cnp+iva)-rit  
    print "           TOTALE: ", italformat(totl)  
    return totl  
# 最终计算报告  
def invoicer(subtotal=None, context=None):  
    if context is None:  
      decimal.getcontext().rounding="ROUND_HALF_UP" # 欧洲截断规则  
    else:  
      decimal.setcontext(context)      
# 设置上下文环境  
    subtot = getsubtotal(subtotal)    
    contrib = cnpcalc(subtot)  
    dototal(subtot, contrib, vatcalc(subtot, contrib), ritacalc(subtot))
    
print "Welcome to the invoice calculator"  
tests = [100, 1000.00, "10000", 555.55]  
print "Euro context"  
for test in tests:  
    invoicer(test)  
print "\n\ndefault context"  
for test in tests:  
    invoicer(test, context=decimal.DefaultContext) 

打印结果如下:

中国
Welcome to the invoice calculator
Euro context


 subtotal:          EUR    100,00
+ contributo integrativo 2%:           2,00
+ IVA 20%:                  20,40
-Ritenuta d'acconto 20%:          20,00
           TOTALE:  EUR    102,40


 subtotal:          EUR  1.000,00
+ contributo integrativo 2%:          20,00
+ IVA 20%:                 204,00
-Ritenuta d'acconto 20%:         200,00
           TOTALE:  EUR  1.024,00


 subtotal:          EUR 10.000,00
+ contributo integrativo 2%:         200,00
+ IVA 20%:               2.040,00
-Ritenuta d'acconto 20%:       2.000,00
           TOTALE:  EUR 10.240,00


 subtotal:          EUR    555,55
+ contributo integrativo 2%:          11,11
+ IVA 20%:                 113,33
-Ritenuta d'acconto 20%:         111,11
           TOTALE:  EUR    568,88




default context


 subtotal:          EUR    100,00
+ contributo integrativo 2%:           2,00
+ IVA 20%:                  20,40
-Ritenuta d'acconto 20%:          20,00
           TOTALE:  EUR    102,40


 subtotal:          EUR  1.000,00
+ contributo integrativo 2%:          20,00
+ IVA 20%:                 204,00
-Ritenuta d'acconto 20%:         200,00
           TOTALE:  EUR  1.024,00


 subtotal:          EUR 10.000,00
+ contributo integrativo 2%:         200,00
+ IVA 20%:               2.040,00
-Ritenuta d'acconto 20%:       2.000,00
           TOTALE:  EUR 10.240,00


 subtotal:          EUR    555,55
+ contributo integrativo 2%:          11,11
+ IVA 20%:                 113,33
-Ritenuta d'acconto 20%:         111,11
           TOTALE:  EUR    568,88

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书山登峰人

精品不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值