Performing Division for the number of plastic-C + + languages will be performed on the floor except for the number of parts. such as int a=15/10; The result of a is 1.
The same is true in Java, so the two int data division needs to force type conversions when it comes to returning a floating-point data, such as float a = (float) b/c, where B and c are int data.
Python is divided into 3 divisions: traditional division, precise division, and floor removal.
Traditional Division
If it is an integer exception to the execution of the floor, if it is a floating-point number, the rule performs precise division.
>>>1/2
0
>>>1.0/2.0
0.5
Exact Division
Division always returns the real quotient, regardless of whether the operand is an integer or a floating-point type. This can be done by executing the from future import Division directive.
#-*-Coding:utf-8-*-from
__future__ Import Division to
__future__ import print_function from
__future_ _ Import unicode_literals
>>>1/2
0.5
>>>1.0/2.0
0.5
Floor except
Starting from Python2.2, an operator//is added to perform the floor except://Division Regardless of the operand is the numeric type, will always give up the decimal part, return the number sequence than the real quotient of the closest number.
>>>1//2
0
>>>1.0//2
0
>>>-1//2.0
-1
Built-in function Divmod ()
Divmod (A,b), Back (a//b,a%b)
>>>divmod (1,2)
(0,1)
>>>divmod (3.14159,1.5)
(2.0,0.4159000000000002)
> >>5+6j//3+2j
2+0j
>>>5+6j%3+2j
-1+2j
>>>divmod (5+6j,3+2j)
(2 +0J), ( -1+2J))
keep two decimal digits
In [1]: a = 5.026 in
[2]: b = 5.000 in
[3]: Round (a,2)
out[3]: 5.03 in
[4]: Round (b,2)
out[4]: 5.0
in [5]: '%.2f '% a
out[5]: ' 5.03 ' in
[6]: '%.2f '% b
out[6]: ' 5.00 ' in
[7]: Float ('%.2f '% a)
out[7] : 5.03 in
[8]: Float ('%.2f '% b)
Out[8]: 5.0 in
[9]: From decimal import decimal in
[ten]: Decimal (' 5.0 ). Quantize (Decimal (' 0.00 '))
out[10]: decimal (' 5.03 ') in
[one]: decimal (' 5.000 '). Quantize (' 0.00 ')
out[11]: Decimal (' 5.00 ')
When the desired output requires two decimal digits, the string form: '%.2f ' is best, followed by the decimal
Round (a,2)
'%.2f '% a
Decimal (' 5.000 '). Quantize (Decimal (' 0.00 '))
A = 1
b = 3 print (A/
b)
#方法一:
print (Round (a/b,2))
#方法二:
print (format float (a)/float (b ), '. 2f ')
#方法三:
print ('%.2f '% (A/b))
Reference Documents
Division in Python
Python retains two decimal digits