This function will round either be order of magnitude (right to left) or by digits the same way that format treats floating point decimal places (left to right:
def intround(n, p):
''' rounds an intger. if "p"<0, p is a exponent of 10; if p>0, left to right digits '''
if p==0: return n
if p>0:
ln=len(str(n))
p=p-ln+1 if n<0 else p-ln
return (n + 5 * 10**(-p-1)) // 10**-p * 10**-p
>>> tgt=5555555
>>> d=2
>>> print('\t{} rounded to {} places:\n\t{} right to left \n\t{} left to right'.format(
tgt,d,intround(tgt,-d), intround(tgt,d)))
Prints
5555555 rounded to 2 places:
5555600 right to left
5600000 left to right
You can also use Decimal class:
import decimal
import sys
def ri(i, prec=6):
ic=long if sys.version_info.major<3 else int
with decimal.localcontext() as lct:
if prec>0:
lct.prec=prec
else:
lct.prec=len(str(decimal.Decimal(i)))+prec
n=ic(decimal.Decimal(i)+decimal.Decimal('0'))
return n
On Python 3 you can reliably use round with negative places and get a rounded integer:
def intround2(n, p):
''' will fail with larger floating point numbers on Py2 and require a cast to an int '''
if p>0:
return round(n, p-len(str(n))+1)
else:
return round(n, p)
On Python 2, round will fail to return a proper rounder integer on larger numbers because round always returns a float:
>>> round(2**34, -5)
17179900000.0 # OK
>>> round(2**64, -5)
1.84467440737096e+19 # wrong
The other 2 functions work on Python 2 and 3