this question has answer here:
- how round off floating number? 9 answers
so ran trouble rounding floats up, code:
foo = float(0.21) bar = float(0.871929) foobar = foo * bar rfoobar = round(foobar,2)
this gives me:
foobar = 0.1831 rfoobar = 0.18
but want rfoobar 0.19
, how accomplish rounds digits when there remainder?
i read math.ceiling
in case doesn't seem trick.
any appreciated.
just move decimal point before , after calling ceil
:
from math import ceil rfoobar = ceil(foobar * 100) / 100
if number of decimals varies, can like:
rfoobar = ceil(foobar * 10 ** digits) / 10 ** digits
Comments
Post a Comment