84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
#<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'
It depends on whether you need the displayed value with two decimal places or the real addition with two decimal places. The former is
ans_print_version = "{.2f}".format(a + b + c)
The latter is
ans = round(a + b + c,2)
http://stackoverflow.com/ques...
It depends on whether you need the displayed value with two decimal places or the real addition with two decimal places.
The former is
The latter is