简体   繁体   English

Python 中数字的四舍五入

[英]Round decimals of numbers in Python

I have a list, each row contains 4 floats (should represent a bounding box)我有一个列表,每行包含 4 个浮点数(应该代表一个边界框)

[[7.426758, 47.398349, 7.850835593464796, 47.68617800490421], 
[7.850835593464796, 47.398349, 8.274913186929592, 47.68617800490421], 
[8.274913186929592, 47.398349, 8.698990780394388, 47.68617800490421]]

I would like to round each float to 6 decimal places.我想将每个浮点数四舍五入到小数点后 6 位。 I tried to round the numbers using pandas, but it also didn't work.我尝试使用 pandas 对数字进行四舍五入,但它也没有用。

{49: '7.850835593464796,49.12532302942521,8.274913186929592,49.413152034329414', 
17: '7.850835593464796,47.9740070098084,8.274913186929592,48.26183601471261', 
71: '10.395301154253572,49.70098103923361,10.819378747718368,49.988810044137814'}

Try numpy.around :尝试numpy.around

# lst is your list
np.around(lst, 6).tolist()

Output: Output:

[[7.426758, 47.398349, 7.850836, 47.686178],
 [7.850836, 47.398349, 8.274913, 47.686178],
 [8.274913, 47.398349, 8.698991, 47.686178]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM