Modify the scale and round.
| 613 | |
| 614 | // Modify the scale and round. |
| 615 | static BasicDecimal128 RoundWithPositiveScale(const BasicDecimalScalar128& x, |
| 616 | int32_t out_precision, int32_t out_scale, |
| 617 | RoundType round_type, bool* overflow) { |
| 618 | DCHECK_GE(out_scale, 0); |
| 619 | |
| 620 | auto scaled = ModifyScaleAndPrecision(x, out_precision, out_scale, overflow); |
| 621 | if (*overflow) { |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | auto delta = ComputeRoundingDelta(x.value(), x.scale(), out_scale, round_type); |
| 626 | if (delta == 0) { |
| 627 | return scaled; |
| 628 | } |
| 629 | |
| 630 | // If there is a rounding delta, the output scale must be less than the input scale. |
| 631 | // That means at least one digit is dropped after the decimal. The delta add can add |
| 632 | // utmost one digit before the decimal. So, overflow will occur only if the output |
| 633 | // precision has changed. |
| 634 | DCHECK_GT(x.scale(), out_scale); |
| 635 | auto result = scaled + delta; |
| 636 | DECIMAL_OVERFLOW_IF(out_precision < x.precision() && |
| 637 | BasicDecimal128::Abs(result) > GetMaxValue(out_precision), |
| 638 | overflow); |
| 639 | return result; |
| 640 | } |
| 641 | |
| 642 | // Modify scale to drop all digits to the right of the decimal and round. |
| 643 | // Then, zero out 'rounding_scale' number of digits to the left of the decimal point. |
no test coverage detected