Debian Codegraph @

beancount_periodic/common/number.py

raw
1 from decimal import Decimal, DecimalException
2 import math
3
4
5 def smart_place_num(ref: Decimal, div: Decimal):
6 round_place_added = int(round(math.log10(abs(div)), 0)) + 1 if div != 0 else 0
7 return min(max(- ref.as_tuple().exponent + round_place_added, 2), 6)
8
9
10 def round_and_remainder(target: Decimal, place_num):
11 try:
12 round_value = round(target, place_num)
13 remainder = target - round_value
14 return round_value, remainder
15 except DecimalException as e:
16 print(e)
17 return target, 0
18
19
20 def remove_exponent_zero(num: Decimal):
21 integral = num.to_integral()
22 return integral if integral == num else num.normalize()