您的位置:首页技术文章
文章详情页

python如何相加加法

【字号: 日期:2022-08-11 14:13:17浏览:43作者:猪猪

问题描述

#<type ’unicode’>a = ’276.30’b = ’1,446.90’c = ’23,456.80’

相加之后,保留2个小数点~

问题解答

回答1:

>>> 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’回答2:

http://stackoverflow.com/ques...

回答3:

>>> 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’回答4:

要看你需要的是显示出来的保留两位小数,还是真实的保留两位小数相加。前者是

ans_print_version = '{.2f}'.format(a + b + c)

后者是

ans = round(a + b + c,2)

标签: Python 编程
相关文章: