0

so i had this problem where i generated numbers from 0-1 with a step of 0.01 but the numbers have so many decimals like 0.010101010101.I only need it in the form 0.01 with two decimals.

How do i remove the rest of decimals ?

  • How exactly are you generating these numbers? What you observe can't be explained by the round-off error implicit in floats. You seem to be doing some sort of weird string concatenation. Either that or this in another duplicate of [is floating point math broken?](https://stackoverflow.com/q/588004/4996248) – John Coleman Apr 09 '20 at 14:02

3 Answers3

0

You can format the decimal like

your_value = 0.1010101010
desired_value = '%.2f' % your_value

Now desired_value will have the decimal upto 2 places.

dxillar
  • 307
  • 2
  • 9
0

Or you could use decimal. Look at the documentation in the link: https://docs.python.org/3/library/decimal.html

0

Round num to digits places. round(num, digits)

David Smolinski
  • 514
  • 3
  • 13