Round self to the nearest integer, or to a given precision. If only one argument is supplied, round a finite Decimal instance self to the nearest integer. If self is infinite or a NaN then a Python exception is raised. If self is finite and lies exactly halfway bet
(self, n=None)
| 1782 | ) |
| 1783 | |
| 1784 | def __round__(self, n=None): |
| 1785 | """Round self to the nearest integer, or to a given precision. |
| 1786 | |
| 1787 | If only one argument is supplied, round a finite Decimal |
| 1788 | instance self to the nearest integer. If self is infinite or |
| 1789 | a NaN then a Python exception is raised. If self is finite |
| 1790 | and lies exactly halfway between two integers then it is |
| 1791 | rounded to the integer with even last digit. |
| 1792 | |
| 1793 | >>> round(Decimal('123.456')) |
| 1794 | 123 |
| 1795 | >>> round(Decimal('-456.789')) |
| 1796 | -457 |
| 1797 | >>> round(Decimal('-3.0')) |
| 1798 | -3 |
| 1799 | >>> round(Decimal('2.5')) |
| 1800 | 2 |
| 1801 | >>> round(Decimal('3.5')) |
| 1802 | 4 |
| 1803 | >>> round(Decimal('Inf')) |
| 1804 | Traceback (most recent call last): |
| 1805 | ... |
| 1806 | OverflowError: cannot round an infinity |
| 1807 | >>> round(Decimal('NaN')) |
| 1808 | Traceback (most recent call last): |
| 1809 | ... |
| 1810 | ValueError: cannot round a NaN |
| 1811 | |
| 1812 | If a second argument n is supplied, self is rounded to n |
| 1813 | decimal places using the rounding mode for the current |
| 1814 | context. |
| 1815 | |
| 1816 | For an integer n, round(self, -n) is exactly equivalent to |
| 1817 | self.quantize(Decimal('1En')). |
| 1818 | |
| 1819 | >>> round(Decimal('123.456'), 0) |
| 1820 | Decimal('123') |
| 1821 | >>> round(Decimal('123.456'), 2) |
| 1822 | Decimal('123.46') |
| 1823 | >>> round(Decimal('123.456'), -2) |
| 1824 | Decimal('1E+2') |
| 1825 | >>> round(Decimal('-Infinity'), 37) |
| 1826 | Decimal('NaN') |
| 1827 | >>> round(Decimal('sNaN123'), 0) |
| 1828 | Decimal('NaN123') |
| 1829 | |
| 1830 | """ |
| 1831 | if n is not None: |
| 1832 | # two-argument form: use the equivalent quantize call |
| 1833 | if not isinstance(n, int): |
| 1834 | raise TypeError('Second argument to round should be integral') |
| 1835 | exp = _dec_from_triple(0, '1', -n) |
| 1836 | return self.quantize(exp) |
| 1837 | |
| 1838 | # one-argument form |
| 1839 | if self._is_special: |
| 1840 | if self.is_nan(): |
| 1841 | raise ValueError("cannot round a NaN") |
nothing calls this directly
no test coverage detected