简体   繁体   中英

Round decimals of numbers in Python

I have a list, each row contains 4 floats (should represent a bounding box)

[[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. I tried to round the numbers using pandas, but it also didn't work.

{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 :

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

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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