MCPcopy Index your code
hub / github.com/RustPython/RustPython / __round__

Method __round__

Lib/_pydecimal.py:1781–1841  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

quantizeMethod · 0.95
is_nanMethod · 0.95
_rescaleMethod · 0.95
isinstanceFunction · 0.85
_dec_from_tripleFunction · 0.85

Tested by

no test coverage detected