MCPcopy Index your code
hub / github.com/python/cpython / _round

Method _round

Lib/_pydecimal.py:2611–2632  ·  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

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