如何四舍五入 [英] How to round numbers

查看:60
本文介绍了如何四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够将这些数字四舍五入到两个小数位,这些数字都存储在一个由网络爬虫输出的变量中:

4.75324.72944.70564.68228571428574.658684.635224.61198666666674.588984.5660644.543216

我是 Python 新手,所以我不确定.我正在使用 Python 2

解决方案

使用内置函数 ​​round(),例如:

<预><代码>>>>回合(4.7532,2)4.75>>>回合(4.7294,2)4.73

How would I be able to round numbers like these to two decimal places which are all stored in a variable which has been outputted by a web scraper:

4.7532
4.7294
4.7056
4.6822857142857
4.65868
4.63522
4.6119866666667
4.58898
4.566064
4.543216

I am new to python so i'm not sure. I'm using Python 2

解决方案

Use the built-in function round(), example:

>>> round(4.7532,2)
4.75
>>> round(4.7294,2)
4.73

这篇关于如何四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆