Decimal module: decimal means decimal, this module provides decimal floating point arithmetic support
It can be passed to Decimal integer or string parameters, but it cannot be floating-point data, because floating-point data itself is not accurate.
To convert from floating point data to Decimal type
from decimal import *
Decimal.from_float(12.222)
from decimal import *
getcontext().prec = 6
Decimal(1)/Decimal(7)
from decimal import *
Decimal(‘50.5679’).quantize(Decimal(‘0.00’))
from decimal import *
str(Decimal(‘3.40’).quantize(Decimal(‘0.0’)))
Example of decimal processing calculation accuracy problem in Python3
#! /usr/bin/python3
# coding:utf-8import decimal
from decimal import Decimal, getcontext
def demo():"""
Rounding problem:
ROUND_CEILING always goes to infinity and rounds up
ROUND_DOWN always rounds towards 0
ROUND_FLOOR always tends to negative infinity and rounds down
ROUND_HALF_DOWN If the last valid digit is greater than or equal to 5, round towards 0; otherwise, round towards 0
ROUND_HALF_EVEN is similar to ROUND_HALF_DOWN, however, if the last valid digit value is 5, the previous digit will be checked.
Even values will cause the result to be rounded down, and odd values will cause the result to be rounded up
ROUND_HALF_UP is similar to ROUND_HALF_DOWN, but if the last valid digit is 5, the value will be rounded to the opposite direction of 0
ROUND_UP rounded to the opposite direction of 0
ROUND_05UP If the last digit is 0 or 5, round to the opposite direction of 0; otherwise round to 0
"""
# 1. Routine calculation
getcontext().prec =9
r1 =Decimal(1)/Decimal(3)print("r1 ", r1) # r1 0.333333333
# 2. But getcontext().prec will contain all the lengths before the decimal point, and the number of digits after the decimal point cannot be fixed when the front length changes
r2 =Decimal(10)/Decimal(3)print("r2 ", r2) # r2 3.33333333
# 3. If you want to control the number of digits after the decimal point, you need to use decimal.quantize(Decimal('0.00000000')), Note that it cannot exceed getcontext().number of prec
r3 =Decimal(1)/Decimal(3)print("r3 ", r3.quantize(Decimal('0.00000000'))) # r3 0.33333333
r4 =Decimal(10)/Decimal(3)print("r4 ", r4.quantize(Decimal('0.00000000'))) # r4 3.33333333
r5 =Decimal(10)/Decimal(str(1.5))print("r5 ", r5.quantize(Decimal('0.00000000'))) # r5 6.66666667
# 4. Rounded up
getcontext().rounding =getattr(decimal,'ROUND_CEILING') #Always round up towards infinity
r6 =Decimal(10)/Decimal(str(1.5)) # r6 6.66666667print("r6 ", r6.quantize(Decimal('0.00000000')))
r7 =Decimal(10)/Decimal(3) # r7 3.33333334print("r7 ", r7.quantize(Decimal('0.00000000')))
# 5. Round down
getcontext().rounding =getattr(decimal,'ROUND_FLOOR') #Always round down towards infinity
r8 =Decimal(10)/Decimal(str(1.5)) # r8 6.66666666print("r8 ", r8.quantize(Decimal('0.00000000')))
r9 =Decimal(10)/Decimal(3) # r9 3.33333333print("r9 ", r9.quantize(Decimal('0.00000000')))if __name__ =='__main__':demo()
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts