Message 239504 - Python tracker (original) (raw)

Or, speaking of "division with remainder":

n, r = divmod(nom * 10**prec, den)
if r * 5 >= den:
    n += 1
x = str(n)
print('%s.%s' % (x[:-prec], x[-prec:]))

... minus the usual off-by-one that the tests would quickly find :)