Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active October 22, 2024 18:45
Show Gist options
  • Save niccolomineo/3045b561eadfb46a696a4254bb5cc95e to your computer and use it in GitHub Desktop.
Save niccolomineo/3045b561eadfb46a696a4254bb5cc95e to your computer and use it in GitHub Desktop.
Truncate decimal to position with none of the unwanted roundings
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