* Parses a number. * Significant bits of this parse algorithm came from https://github.com/MikeMcl/big.js/
(num: number)
| 395 | * Significant bits of this parse algorithm came from https://github.com/MikeMcl/big.js/ |
| 396 | */ |
| 397 | function parseNumber(num: number): ParsedNumber { |
| 398 | let numStr = Math.abs(num) + ''; |
| 399 | let exponent = 0, |
| 400 | digits, |
| 401 | integerLen; |
| 402 | let i, j, zeros; |
| 403 | |
| 404 | // Decimal point? |
| 405 | if ((integerLen = numStr.indexOf(DECIMAL_SEP)) > -1) { |
| 406 | numStr = numStr.replace(DECIMAL_SEP, ''); |
| 407 | } |
| 408 | |
| 409 | // Exponential form? |
| 410 | if ((i = numStr.search(/e/i)) > 0) { |
| 411 | // Work out the exponent. |
| 412 | if (integerLen < 0) integerLen = i; |
| 413 | integerLen += +numStr.slice(i + 1); |
| 414 | numStr = numStr.substring(0, i); |
| 415 | } else if (integerLen < 0) { |
| 416 | // There was no decimal point or exponent so it is an integer. |
| 417 | integerLen = numStr.length; |
| 418 | } |
| 419 | |
| 420 | // Count the number of leading zeros. |
| 421 | for (i = 0; numStr.charAt(i) === ZERO_CHAR; i++) { |
| 422 | /* empty */ |
| 423 | } |
| 424 | |
| 425 | if (i === (zeros = numStr.length)) { |
| 426 | // The digits are all zero. |
| 427 | digits = [0]; |
| 428 | integerLen = 1; |
| 429 | } else { |
| 430 | // Count the number of trailing zeros |
| 431 | zeros--; |
| 432 | while (numStr.charAt(zeros) === ZERO_CHAR) zeros--; |
| 433 | |
| 434 | // Trailing zeros are insignificant so ignore them |
| 435 | integerLen -= i; |
| 436 | digits = []; |
| 437 | // Convert string to array of digits without leading/trailing zeros. |
| 438 | for (j = 0; i <= zeros; i++, j++) { |
| 439 | digits[j] = Number(numStr.charAt(i)); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // If the number overflows the maximum allowed digits then use an exponent. |
| 444 | if (integerLen > MAX_DIGITS) { |
| 445 | digits = digits.splice(0, MAX_DIGITS - 1); |
| 446 | exponent = integerLen - 1; |
| 447 | integerLen = 1; |
| 448 | } |
| 449 | |
| 450 | return {digits, exponent, integerLen}; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Round the parsed number to the specified number of decimal places |
no test coverage detected
searching dependent graphs…