Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created January 19, 2017 05:51
Show Gist options
  • Save cocodrips/fee93d7652d17fe2b2215497142b0324 to your computer and use it in GitHub Desktop.
Save cocodrips/fee93d7652d17fe2b2215497142b0324 to your computer and use it in GitHub Desktop.
丸めるのと切り下げるの
import decimal
def round_n(value, n):
if n <= 0:
return decimal.Decimal(str(value)).quantize(decimal.Decimal('1.'), rounding=decimal.ROUND_HALF_UP)
return decimal.Decimal(str(value)).quantize(decimal.Decimal('.{}1'.format('0' * (n - 1))), rounding=decimal.ROUND_HALF_UP)
def floor_n(value, n):
if n <= 0:
return decimal.Decimal(str(value)).quantize(decimal.Decimal('1.'), rounding=decimal.ROUND_FLOOR)
return decimal.Decimal(str(value)).quantize(decimal.Decimal('.{}1'.format('0' * (n - 1))), rounding=decimal.ROUND_FLOOR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment