Warm tip: This article is reproduced from serverfault.com, please click

Round a floating point number by a given precision

发布于 2020-11-30 09:17:22

I need to get qty number by a specified precision like below:

qty = 0.1284
precision = 0.01
output = 0.12

It seems easy, buy I could not get it done because I have a precision above variable.

Questioner
tompal18
Viewed
0
Patrick Artner 2020-11-30 17:33:43

Floating math is not precise - see Is floating point math broken?.

You can use round() to round - you seem to want to truncate though - you can do this mathematically:

qty = 0.1284
precision = 0.01 

truncated = ((qty * (1/precision)) // 1 ) * precision

print(truncated)

Output:

0.12

This multiplies your number with the correct factor, integer divides by 1 to get rid of remaining partial digits and multiplies by precision again to get back to your float value.