Skip to content

Instantly share code, notes, and snippets.

@0187773933
Created February 18, 2025 15:33
Show Gist options
  • Save 0187773933/95c0368f179cb996f5212061b0460d42 to your computer and use it in GitHub Desktop.
Save 0187773933/95c0368f179cb996f5212061b0460d42 to your computer and use it in GitHub Desktop.
Python Auto Decimal Wrapper
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