round(self, ndigits) Rounds half toward even.
(self, ndigits=None)
| 968 | return -(-a._numerator // a._denominator) |
| 969 | |
| 970 | def __round__(self, ndigits=None): |
| 971 | """round(self, ndigits) |
| 972 | |
| 973 | Rounds half toward even. |
| 974 | """ |
| 975 | if ndigits is None: |
| 976 | d = self._denominator |
| 977 | floor, remainder = divmod(self._numerator, d) |
| 978 | if remainder * 2 < d: |
| 979 | return floor |
| 980 | elif remainder * 2 > d: |
| 981 | return floor + 1 |
| 982 | # Deal with the half case: |
| 983 | elif floor % 2 == 0: |
| 984 | return floor |
| 985 | else: |
| 986 | return floor + 1 |
| 987 | shift = 10**abs(ndigits) |
| 988 | # See _operator_fallbacks.forward to check that the results of |
| 989 | # these operations will always be Fraction and therefore have |
| 990 | # round(). |
| 991 | if ndigits > 0: |
| 992 | return Fraction(round(self * shift), shift) |
| 993 | else: |
| 994 | return Fraction(round(self / shift) * shift) |
| 995 | |
| 996 | def __hash__(self): |
| 997 | """hash(self)""" |