Home > front end >  Round decimals of numbers in Python
Round decimals of numbers in Python

Time:11-26

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'}

CodePudding user response:

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