Rounds to a nearby integer. If no rounding mode is specified, take the rounding mode from the context. This method raises the Rounded and Inexact flags when appropriate. See also: to_integral_value, which does exactly the same as this method except that it
(self, rounding=None, context=None)
| 2632 | return ans |
| 2633 | |
| 2634 | def to_integral_exact(self, rounding=None, context=None): |
| 2635 | """Rounds to a nearby integer. |
| 2636 | |
| 2637 | If no rounding mode is specified, take the rounding mode from |
| 2638 | the context. This method raises the Rounded and Inexact flags |
| 2639 | when appropriate. |
| 2640 | |
| 2641 | See also: to_integral_value, which does exactly the same as |
| 2642 | this method except that it doesn't raise Inexact or Rounded. |
| 2643 | """ |
| 2644 | if self._is_special: |
| 2645 | ans = self._check_nans(context=context) |
| 2646 | if ans: |
| 2647 | return ans |
| 2648 | return Decimal(self) |
| 2649 | if self._exp >= 0: |
| 2650 | return Decimal(self) |
| 2651 | if not self: |
| 2652 | return _dec_from_triple(self._sign, '0', 0) |
| 2653 | if context is None: |
| 2654 | context = getcontext() |
| 2655 | if rounding is None: |
| 2656 | rounding = context.rounding |
| 2657 | ans = self._rescale(0, rounding) |
| 2658 | if ans != self: |
| 2659 | context._raise_error(Inexact) |
| 2660 | context._raise_error(Rounded) |
| 2661 | return ans |
| 2662 | |
| 2663 | def to_integral_value(self, rounding=None, context=None): |
| 2664 | """Rounds to the nearest integer, without raising inexact, rounded.""" |