Skip to content

Instantly share code, notes, and snippets.

@gyli
Created December 14, 2021 05:35
Show Gist options
  • Save gyli/b9691139999c15b0cf9f7dfabedf0cd6 to your computer and use it in GitHub Desktop.
Save gyli/b9691139999c15b0cf9f7dfabedf0cd6 to your computer and use it in GitHub Desktop.
Calculate range of Decimal with precision and scale
from typing import Decimal, Tuple
def calculate_decimal_range(precision: int, scale: int) -> Tuple[Decimal, Decimal]:
"""
This method calculates the range of Decimal with given precision and scale.
:return: (min_value, max_value)
"""
precision, scale = Decimal(precision), Decimal(scale)
max_value = 10**(precision-scale) - 10**-scale
return -max_value, max_value
# Input:
# precision=10
# scale=5
# Output:
# (Decimal('-99999.99999'), Decimal('99999.99999'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment