Created
January 19, 2017 05:51
-
-
Save cocodrips/fee93d7652d17fe2b2215497142b0324 to your computer and use it in GitHub Desktop.
丸めるのと切り下げるの
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
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