Skip to content

Instantly share code, notes, and snippets.

@dcragusa
Created September 19, 2018 12:03
Show Gist options
  • Save dcragusa/b8805a05ba9c07cb88ca2b166d417217 to your computer and use it in GitHub Desktop.
Save dcragusa/b8805a05ba9c07cb88ca2b166d417217 to your computer and use it in GitHub Desktop.
Quick and easy access to the Decimal module, with number of d.p.'s
# Avoid 'D = decimal.Decimal' crud
from decimal import Decimal
from typing import Union as U
def decimalize(num: float, digits_str: str) -> Decimal:
return Decimal(num).quantize(Decimal(digits_str))
def no_dp(num: U[float, Decimal]) -> Decimal:
return decimalize(num, '1.0')
def one_dp(num: U[float, Decimal]) -> Decimal:
return decimalize(num, '0.1')
def two_dp(num: U[float, Decimal]) -> Decimal:
return decimalize(num, '0.01')
def three_dp(num: U[float, Decimal]) -> Decimal:
return decimalize(num, '0.001')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment