文章详情页
python如何相加加法
问题描述
#<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)
相关文章:
1. javascript - prismplayer播放器全屏问题2. python相关问题求解决,有偿3. python3.x - mongodb 显示不出中文,都是方块,该如何解决?4. javascript - vue2.0中router.repalce跟router.push有什么区别,二者的应用场景是会有怎样的区别?5. javascript - 在sublime中如何给.vue文件注释?6. HTML5怎么访问本地文件?7. javascript - 这种上传图片预览怎么做?8. javascript - input输入框触发change事件后,外部元素会被选中9. javascript - 把字符串日期转换成一般时间格式后,发现小于10的数字不会自动添加0在前面,如下10. javascript - js判断一个数组是否重复
排行榜