Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import decimal
- def truncate(num, lim):
- """Truncates/pads a float f to n decimal places without rounding."""
- snum = "{}".format(num)
- if 'e' in snum or 'E' in snum:
- snum = "{0:f}".format(num)
- (intg, pt, dec) = snum.partition('.')
- numlen = len(intg) + len(dec)
- if len(intg) < lim:
- if numlen >= lim:
- diff = lim - len(intg)
- fnum = intg + '.' + dec[0:diff]
- else:
- fnum = intg + '.' + dec + '0' * (lim - numlen)
- elif len(intg) == lim:
- fnum = intg
- else:
- fnum = intg[0:lim]
- return decimal.Decimal(fnum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement