Last active
October 22, 2024 18:45
-
-
Save niccolomineo/3045b561eadfb46a696a4254bb5cc95e to your computer and use it in GitHub Desktop.
Truncate decimal to position with none of the unwanted roundings
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
def trunc_decimal(number: Decimal | float, position: int): | |
""" | |
Truncate decimal to position with none of the unwanted roundings. | |
:param position: decimal position to truncate the decimal to | |
:return: decimal truncated to position | |
""" | |
coefficient = "1" + "0" * position | |
return floor(number * int(coefficient)) / int(coefficient) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment