Created
February 18, 2025 15:33
-
-
Save 0187773933/95c0368f179cb996f5212061b0460d42 to your computer and use it in GitHub Desktop.
Python Auto Decimal Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal, getcontext | |
getcontext().prec = 28 | |
class D(Decimal): | |
def __new__(cls, value): | |
if isinstance(value, (float, int)): | |
return super().__new__(cls, str(value)) | |
return super().__new__(cls, value) | |
print(D(115.412) - D(115.213)) # Decimal('0.199') | |
print(D("115.412") - D("115.213")) # Decimal('0.199') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment