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 halfwa
(self, n=None)
| 1838 | ) |
| 1839 | |
| 1840 | def __round__(self, n=None): |
| 1841 | """Round self to the nearest integer, or to a given precision. |
| 1842 | |
| 1843 | If only one argument is supplied, round a finite Decimal |
| 1844 | instance self to the nearest integer. If self is infinite or |
| 1845 | a NaN then a Python exception is raised. If self is finite |
| 1846 | and lies exactly halfway between two integers then it is |
| 1847 | rounded to the integer with even last digit. |
| 1848 | |
| 1849 | >>> round(Decimal('123.456')) |
| 1850 | 123 |
| 1851 | >>> round(Decimal('-456.789')) |
| 1852 | -457 |
| 1853 | >>> round(Decimal('-3.0')) |
| 1854 | -3 |
| 1855 | >>> round(Decimal('2.5')) |
| 1856 | 2 |
| 1857 | >>> round(Decimal('3.5')) |
| 1858 | 4 |
| 1859 | >>> round(Decimal('Inf')) |
| 1860 | Traceback (most recent call last): |
| 1861 | ... |
| 1862 | OverflowError: cannot round an infinity |
| 1863 | >>> round(Decimal('NaN')) |
| 1864 | Traceback (most recent call last): |
| 1865 | ... |
| 1866 | ValueError: cannot round a NaN |
| 1867 | |
| 1868 | If a second argument n is supplied, self is rounded to n |
| 1869 | decimal places using the rounding mode for the current |
| 1870 | context. |
| 1871 | |
| 1872 | For an integer n, round(self, -n) is exactly equivalent to |
| 1873 | self.quantize(Decimal('1En')). |
| 1874 | |
| 1875 | >>> round(Decimal('123.456'), 0) |
| 1876 | Decimal('123') |
| 1877 | >>> round(Decimal('123.456'), 2) |
| 1878 | Decimal('123.46') |
| 1879 | >>> round(Decimal('123.456'), -2) |
| 1880 | Decimal('1E+2') |
| 1881 | >>> round(Decimal('-Infinity'), 37) |
| 1882 | Decimal('NaN') |
| 1883 | >>> round(Decimal('sNaN123'), 0) |
| 1884 | Decimal('NaN123') |
| 1885 | |
| 1886 | """ |
| 1887 | if n is not None: |
| 1888 | # two-argument form: use the equivalent quantize call |
| 1889 | if not isinstance(n, int): |
| 1890 | raise TypeError('Second argument to round should be integral') |
| 1891 | exp = _dec_from_triple(0, '1', -n) |
| 1892 | return self.quantize(exp) |
| 1893 | |
| 1894 | # one-argument form |
| 1895 | if self._is_special: |
| 1896 | if self.is_nan(): |
| 1897 | raise ValueError("cannot round a NaN") |
nothing calls this directly
no test coverage detected