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

Method _round

Lib/_pydecimal.py:2608–2629  ·  view source on GitHub ↗

Round a nonzero, nonspecial Decimal to a fixed number of significant figures, using the given rounding mode. Infinities, NaNs and zeros are returned unaltered. This operation is quiet: it raises no flags, and uses no information from the context.

(self, places, rounding)

Source from the content-addressed store, hash-verified

2606 return _dec_from_triple(self._sign, coeff, exp)
2607
2608 def _round(self, places, rounding):
2609 """Round a nonzero, nonspecial Decimal to a fixed number of
2610 significant figures, using the given rounding mode.
2611
2612 Infinities, NaNs and zeros are returned unaltered.
2613
2614 This operation is quiet: it raises no flags, and uses no
2615 information from the context.
2616
2617 """
2618 if places <= 0:
2619 raise ValueError("argument should be at least 1 in _round")
2620 if self._is_special or not self:
2621 return Decimal(self)
2622 ans = self._rescale(self.adjusted()+1-places, rounding)
2623 # it can happen that the rescale alters the adjusted exponent;
2624 # for example when rounding 99.97 to 3 significant figures.
2625 # When this happens we end up with an extra 0 at the end of
2626 # the number; a second rescale fixes this.
2627 if ans.adjusted() != self.adjusted():
2628 ans = ans._rescale(ans.adjusted()+1-places, rounding)
2629 return ans
2630
2631 def to_integral_exact(self, rounding=None, context=None):
2632 """Rounds to a nearby integer.

Callers 1

__format__Method · 0.95

Calls 3

_rescaleMethod · 0.95
adjustedMethod · 0.95
DecimalClass · 0.85

Tested by

no test coverage detected