Oct-20-2022, 06:34 PM
(This post was last modified: Oct-21-2022, 03:02 PM by deanhystad.)
So you want decimal to resize to a minimum precision instead of remembering the precision it inherited from a math operation.
One way to make a new decimal with all trailing zeros removed.
from decimal import Decimal as D a = D("3") b = D("2") c = b / a d = c * a print(a, b, c, d)
Output:3 2 0.6666666666666666666666666667 2.000000000000000000000000000
d should be 2 instead of 2.000000000000000000000000000One way to make a new decimal with all trailing zeros removed.
from decimal import * from decimal import Decimal as D def rstrip(value:D)->D: """I'm a happy little function :D with a tragic ending D:""" value_str = str(value) if "." in value_str: return D(value_str.rstrip(".0")) return value a = D("3") b = D("2") c = rstrip(b / a) d = rstrip(c * a) print(a, b, c, d, D(1000))
Output:3 2 0.6666666666666666666666666667 2 1000