table of Contents
@
Python in rounding very strange, is said to have such a rule: "The four round split the banker", "five bisecting" is based on the number of digits before the decimal parity-offs to determine the parity equally, in line with the principle of equity (rounding not fair).
1 round () method
Built-in function round (x [, n]), if not return to the integer value of n. (Based on the following code python3.7)
>>> round(2.685)
3
>>>
>>> round(2.655,2)
2.65
>>> round(2.685,2)
2.69
>>>
Method 2 format string
2.1 format formatting functions
It was a long time ago I love when a format string, the% operator met behind, I faithless. Of course I could not forget it, see it in the code below rounding function.
>>> "{:.2f}".format(2.655)
'2.65'
>>> "{:.2f}".format(2.685)
'2.69'
Operators 2.2%
% Operator is used up very smoothly wow, wow worthy of love!
>>> "%.2f"%2.655
'2.65'
>>> "%.2f"%2.685
'2.68'
3 summary
There may be other ways, but after the test, the findings not only with the number of digits before the decimal parity-offs, but also with reservations related to several decimal places! Want to pursue teacher of rounding, or write your own function bar, built-in method is somewhat different!