I ran into a problem writing a program today about how to control floating-point numbers only show two digits after the decimal point, the normal idea is to use the round function, for exampleround(a, 2),但是在面对下面的问题时候round就不太好用了
>>> a=13.949999999999999
>>> Round (A, 2)
13.949999999999999
On the Internet to check the information, there are netizens to provide a way
Print ('%.2f '%a)
>>>13.95
Alternatively, you can use Trunc (a,2) to intercept the function
>>>13.94
You can also use the decimal
Decimal. The decimal type is a data type that satisfies high-precision calculations in Python and imports the decimal package with import
Define the decimal data type:
1 cannot be defined in the way that the literal value is used
2 are defined in the following ways:
>>> Import decimal >>> x = Decimal. Decimal (87345) >>> x decimal (' 87345 ') >>> x = Decimal. Decimal (' 123.3344332566334 ') >>> x decimal (' 123.3344332566334 ') can be passed to a Decimal integer or string parameter, but cannot be a floating-point data, Because the floating-point data itself is inaccurate
If you need to convert from floating-point data to a decimal type, you might use the following methods
>>> x = Decimal. Decimal.from_float (127.3323)
>>> x
Decimal (' 127.332300000000003592504072003066539764404296875 ')
I had 3 questions pertaining to decimal arithmetic in Python, and all 3 of the which is best asked inline:
1)
FromImport,Decimal>>>().=6>>>Decimal' 50.567898491579878 ' 1Decimal' 50.5679 '# How are this a precision of 6? If the decimal counts whole numbersas >>># Part of the precision, are that actually still precision? >>> and
2)
FromImport,Decimal>>>().=6>>>Decimal' 50.567898491579878 'Decimal' 50.567898491579878 '# shouldn ' t that has been rounded to 6 digits on instantiation?>>>Decimal' 50.567898491579878 ' 1decimal ' 50.5679 ' >>>3)
# now I want to save the value to my database as a ' total ' with 2 places.>>>FromImportDecimal>>># is the following the correct-to-get the value into 2 decimal places,>>># or is there a "better"?>>>=Decimal' 50.5679 'QuantizeDecimal' 0.00 '# Just wanted to see what's the value wasDecimal' 50.57 '()>>> Native test cases:
[Python]View Plain copy
- >>> Import Decimal
- >>> x = Decimal. Decimal (87345)
- >>> x
- Decimal (' 87345 ')
- >>> Print x
- 87345
- >>> from decimal import getcontext, decimal
- >>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.00 '))
- >>> x
- Decimal (' 1.00 ')
- >>> Print x
- 1.00
- >>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.0000 '))
- >>> x
- Decimal (' 0.9985 ')
- >>> Print x
- 0.9985
- >>> y = decimal.from_float (0.998531571219)
- >>> y
- Decimal (' 0.99853157121900004700165709436987526714801788330078125 ')
- >>> y = decimal.from_float (0.998531571219). Quantize (Decimal (' 0.0000 '))
- >>> y
- Decimal (' 0.9985 ')
- >>> Print y
- 0.9985
- >>> f1 = 0.998531571219
- >>> F1
- 0.998531571219
- >>> Type (F1)
- <type ' float ' >
- >>> F2 = str (f1)
- >>> F2
- ' 0.998531571219 '
- >>> type (F2)
- <type ' str ' >
- >>>
Two decimal places reserved in Python