rounds floats in more human readable format e.g.
341.123434 mUSD -> 341mUSD
0.45345345 mUSD -> 0.5 mUSD
0.034545 mUSD -> 0.03 mUSD
0.0014545 mUSD -> 0.001 mUSD
etc.
@register.filter(is_safe=True)defsmartround(num):""" rounds floats in more human readable format e.g. 341.123434 mUSD -> 341mUSD 0.45345345 mUSD -> 0.5mUSD 0.034545 mUSD -> 0.03mUSD 0.0014545 mUSD -> 0.001mUSD etc. """ifisinstance(num,float):whole_part,dec_part=str(num).split(".")whole_part_len=len(whole_part)ifwhole_part_len>3:returnint(num)else:try:round_to=re.search(r'[^0]',dec_part).start()returnround(num,round_to+1)exceptAttributeError:returnint(num)returnnum
Comments
Please login first before commenting.