python - How can I correct this rounding down function? -
how can correct rounding down function?
def round_down(num, prec): uod = round(num, prec) if uod > num: return uod - 1/10^prec return uod
it raises: typeerror: unsupported operand type(s) ^: 'float' , 'int'.
^
not mean think means. use **
instead.
the
^
operator yields bitwise xor (exclusive or) of arguments, must plain or long integers.
also, mgilson noted, 1/10
equal 0
in python 2.x, want use 1.0/10
instead:
def round_down(num, prec): uod = round(num, prec) if uod > num: return uod - 1.0/10 ** prec return uod
Comments
Post a Comment