Round a floating point number by a given precision

Viewed 417

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.

2 Answers

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.

I have done it like below.

def get_qty(qty, step_size):

    prec = 0
    if(step_size >= 1):
        return int(qty)
    while(step_size < 1):
        step_size *= 10
        prec += 1
    qty = "{:0.0{}f}".format(qty, prec)
    return float(qty)
get_qty(181.1231231221, 0.01)
get_qty(181.1231231221, 1.0)
Related