function toNonScientific()

in src/formattingService/formattingService.ts [804:853]


    function toNonScientific(value: number, precision: number): string {
        let result = "";
        let precisionZeros = 0;
        // Double precision numbers support actual 15-16 decimal digits of precision.
        if (precision > 16) {
            precisionZeros = precision - 16;
            precision = 16;
        }
        let digitsBeforeDecimalPoint = Double.log10(Math.abs(value));
        if (digitsBeforeDecimalPoint < 16) {
            if (digitsBeforeDecimalPoint > 0) {
                let maxPrecision = 16 - digitsBeforeDecimalPoint;
                if (precision > maxPrecision) {
                    precisionZeros += precision - maxPrecision;
                    precision = maxPrecision;
                }
            }
            result = value.toFixed(precision);
        } else if (digitsBeforeDecimalPoint === 16) {
            result = value.toFixed(0);
            precisionZeros += precision;
            if (precisionZeros > 0) {
                result += ".";
            }
        } else { // digitsBeforeDecimalPoint > 16
            // Different browsers have different implementations of the toFixed().
            // In IE it returns fixed format no matter what's the number. In FF and Chrome the method returns exponential format for numbers greater than 1E21.
            // So we need to check for range and convert the to exponential with the max precision.
            // Then we convert exponential string to fixed by removing the dot and padding with "power" zeros.
            // Assert that value is a number and fall back on returning value if it is not
            if (typeof (value) !== "number")
                return String(value);
            result = value.toExponential(15);
            let indexOfE = result.indexOf("e");
            if (indexOfE > 0) {
                let indexOfDot = result.indexOf(".");
                let mantissa = result.substr(0, indexOfE);
                let exp = result.substr(indexOfE + 1);
                let powerZeros = parseInt(exp, 10) - (mantissa.length - indexOfDot - 1);
                result = mantissa.replace(".", "") + stringExtensions.repeat("0", powerZeros);
                if (precision > 0) {
                    result = result + "." + stringExtensions.repeat("0", precision);
                }
            }
        }
        if (precisionZeros > 0) {
            result = result + stringExtensions.repeat("0", precisionZeros);
        }
        return result;
    }