(self: BigDecimal)
| 144 | * @category scaling |
| 145 | */ |
| 146 | export const normalize = (self: BigDecimal): BigDecimal => { |
| 147 | if (self.normalized === undefined) { |
| 148 | if (self.value === bigint0) { |
| 149 | self.normalized = zero |
| 150 | } else { |
| 151 | const digits = `${self.value}` |
| 152 | |
| 153 | let trail = 0 |
| 154 | for (let i = digits.length - 1; i >= 0; i--) { |
| 155 | if (digits[i] === "0") { |
| 156 | trail++ |
| 157 | } else { |
| 158 | break |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (trail === 0) { |
| 163 | self.normalized = self |
| 164 | } |
| 165 | |
| 166 | const value = BigInt(digits.substring(0, digits.length - trail)) |
| 167 | const scale = self.scale - trail |
| 168 | self.normalized = unsafeMakeNormalized(value, scale) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return self.normalized |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Scales a given `BigDecimal` to the specified scale. |
no test coverage detected