Created
September 19, 2018 12:03
-
-
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
This file contains 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
# 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