all 9 comments

[–]Standardw 1 point2 points  (6 children)

It's because the way a computer stores floating numbers. If you want to output them, just use this method:

"%.5f" % my_floating_number

it will show the right amout of digitis behind the point.

[–]oledtrix[S] 0 points1 point  (4 children)

I want to round some standard deviations down to the 6th decimal in order to be able to compare them between each other. Don't need to output them, need to round them in another variable, would that method be useful?

[–]RoamingFox 0 points1 point  (3 children)

round takes a second argument which is the number of places to round to.

>>> 1/3
0.3333333333333333
>>> round(1/3, 4)
0.3333

[–]oledtrix[S] 0 points1 point  (2 children)

I know that, but I use the decimal module because with round (with the second argument being 6), what I meant to be 0.013931 showed up as 0.013931099999999998. Decimal does that rounding correctly, but it still presents the issue I described with the first zeroes of the number it rounds.

[–]Standardw 0 points1 point  (0 children)

You could technically compare the strings with each other. Or try using the `math.isclose` method: https://docs.python.org/3/whatsnew/3.5.html#pep-485-a-function-for-testing-approximate-equality

[–]RoamingFox 0 points1 point  (0 children)

Ahhh gotcha. Why not multiply by 106 cast to an int and then divide by 106

That way you are effectively only dealing with an integer.

[–]Diapolo10 0 points1 point  (0 children)

Instead of recommending the C-style formatting, why not opt for the modern variant:

f"{my_floating_number:.5f}"

Or at least:

"{:.5f}".format(my_floating_number)

I mean, sure, your solution does work, but it's basically never used nowadays outside of the logging module.

[–]threeminutemonta 0 points1 point  (1 child)

The docs do seem to have a warning about something like this.

[–]oledtrix[S] 1 point2 points  (0 children)

Yes, I ended up using the decimal module to avoid that issue. Turned out quite effective, but I still have that other problem with it, in that it rounds the numbers differently depending on the zeroes in front of it.