#<type 'unicode'> a = '276.30' b = '1,446.90' c = '23,456.80'
相加之后,保留2个小数点~
走同样的路,发现不同的人生
>>> num=['276.30','1,446.90','23,456.80'] >>> "%.2f" % sum(map(lambda s:float(s.replace(',','')),num)) '25180.00'
>>> '{:,.2f}'.format(sum(map(lambda s:float(s.replace(',','')),num))) '25,180.00'
http://stackoverflow.com/ques...
>>> def sum(*args): ... r = 0.0 ... for n in args: ... r += float(n) ... return "%.2f" % r ... >>> sum('1.1', '2.2') '3.30' >>> sum('1.1', '2.2', 3.3) '6.60'
要看你需要的是显示出来的保留两位小数,还是真实的保留两位小数相加。前者是
ans_print_version = "{.2f}".format(a + b + c)
后者是
ans = round(a + b + c,2)
http://stackoverflow.com/ques...
要看你需要的是显示出来的保留两位小数,还是真实的保留两位小数相加。
前者是
后者是