* Converts the UInt64 to a string written in the specified radix * @this {!UInt64} * @param {number=} radix Radix (2-36), defaults to 10 * @returns {string} Result string
(radix = 10)
| 1200 | * @returns {string} Result string |
| 1201 | */ |
| 1202 | toString (radix = 10) { |
| 1203 | radix = radix || 10 |
| 1204 | if ((radix < 2) || (radix > 36)) { |
| 1205 | throw new RangeError('Radix must be in range [2, 36]') |
| 1206 | } |
| 1207 | if (this.isZero) { |
| 1208 | return '0' |
| 1209 | } |
| 1210 | if (this.isNegative) { |
| 1211 | if (this.eq(UINT64_MIN)) { |
| 1212 | // We need to change the Long value before it can be negated, so we remove |
| 1213 | // the bottom-most digit in this base and then recurse to do the rest. |
| 1214 | let radixLong = UInt64.fromNumber(radix) |
| 1215 | let div = this.div(radixLong) |
| 1216 | let rem1 = div.mul(radixLong).sub(this) |
| 1217 | return div.toString(radix) + rem1.toInt32().toString(radix) |
| 1218 | } else { |
| 1219 | return '-' + this.neg().toString(radix) |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // Do several (6) digits each time through the loop, so as to |
| 1224 | // minimize the calls to the very expensive emulated div. |
| 1225 | let radixToPower = UInt64.fromNumber(Math.pow(radix, 6)) |
| 1226 | let rem = this |
| 1227 | let result = '' |
| 1228 | while (true) { |
| 1229 | let remDiv = rem.div(radixToPower) |
| 1230 | let intval = rem.sub(remDiv.mul(radixToPower)).toInt32() >>> 0 |
| 1231 | let digits = intval.toString(radix) |
| 1232 | rem = remDiv |
| 1233 | if (rem.isZero) { |
| 1234 | return digits + result |
| 1235 | } else { |
| 1236 | while (digits.length < 6) { |
| 1237 | digits = '0' + digits |
| 1238 | } |
| 1239 | result = '' + digits + result |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /** |
| 1245 | * Converts the UInt64 to Int64 |