Round this Decimal128 to the specified scale using the given rounding mode. This method performs in-place rounding without requiring a divisor. @param targetScale the desired scale (number of decimal places) @param roundingMode the rounding mode to use @throws NumericException if targetScale is in
(int targetScale, RoundingMode roundingMode)
| 1075 | * @throws NumericException if roundingMode is UNNECESSARY and rounding is required |
| 1076 | */ |
| 1077 | public void round(int targetScale, RoundingMode roundingMode) { |
| 1078 | if (isNull()) { |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | if (targetScale == this.scale) { |
| 1083 | // No rounding needed |
| 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | validateScale(targetScale); |
| 1088 | |
| 1089 | if (isZero()) { |
| 1090 | this.scale = targetScale; |
| 1091 | return; |
| 1092 | } |
| 1093 | |
| 1094 | if (this.scale < targetScale) { |
| 1095 | boolean isNegative = isNegative(); |
| 1096 | if (isNegative) { |
| 1097 | negate(); |
| 1098 | } |
| 1099 | |
| 1100 | // Need to increase scale (add trailing zeros) |
| 1101 | int scaleIncrease = targetScale - this.scale; |
| 1102 | multiplyByPowerOf10InPlace(scaleIncrease); |
| 1103 | this.scale = targetScale; |
| 1104 | |
| 1105 | if (isNegative) { |
| 1106 | negate(); |
| 1107 | } |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | divide(0, 1, 0, targetScale, roundingMode); |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Set the scale forcefully without doing any rescaling operations |