代码如下:
#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