Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from decimal import Decimal
- >>> Decimal('1.0') + Decimal('2.0')
- Decimal("3.0")
- >>> Decimal('1.00') / Decimal('3.00')
- Decimal("0.3333333333333333333333333333")
- decimal.getcontext().prec=2
- decimal.getcontext().prec=2
- >>> "%8.2e"% ( 1.0/3.0 )
- '3.33e-01'
- def lround(x,leadingDigits=0):
- """Return x either as 'print' would show it (the default)
- or rounded to the specified digit as counted from the leftmost
- non-zero digit of the number, e.g. lround(0.00326,2) --> 0.0033
- """
- assert leadingDigits>=0
- if leadingDigits==0:
- return float(str(x)) #just give it back like 'print' would give it
- return float('%.*e' % (int(leadingDigits),x)) #give it back as rounded by the %e format
- >>> lround(1./3.,2),str(lround(1./3.,2)),str(lround(1./3.,4))
- (0.33000000000000002, '0.33', '0.3333')
Add Comment
Please, Sign In to add comment