Javascript toFixed function

前端 未结 6 1743
春和景丽
春和景丽 2020-12-30 04:51

The expected result of:

(1.175).toFixed(2) = 1.18 and
(5.175).toFixed(2) = 5.18

But in JS showing:

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 05:34

    Kippie your solution has problems one of them

    39133.005.toFixed(2) => 39133 
    
    var Calc = function () {
        var self = this;
    
    this.float2Array = function(num) {
        var floatString = num.toString(),
            exp = floatString.indexOf(".") - (floatString.length - 1),
            mant = floatString.replace(".", "").split("").map(function (i) { return parseInt(i); });
        return { exp: exp, mant: mant };
    };
    
    this.round2 = function (num, dec, sep) {
        var decimal = !!dec ? dec : 2,
        separator = !!sep ? sep : '',
        r = parseFloat(num),
        exp10 = Math.pow(10, decimal);
        r = Math.round(r * exp10) / exp10;
    
        var rr = Number(r).toFixed(decimal).toString().split('.');
    
        var b = rr[0].replace(/(\d{1,3}(?=(\d{3})+(?:\.\d|\b)))/g, "\$1" + separator);
        r = (rr[1] ? b + '.' + rr[1] : b);
    
        return r;
    };
    
    this.toFixed10 = function (f, num) {
        var prepareInt = self.float2Array(f),
            naturalInt = prepareInt.mant,
            places = Math.abs(prepareInt.exp),
            result = prepareInt.mant.slice(),
            resultFixedLenth;
    
        // if number non fractional or has zero fractional part
        if (f.isInt()) {
            return f.toFixed(num);
        }
        // if the number of decimal places equals to required rounding
        if (places === num) {
            return Number(f).toString();
        }
        //if number has trailing zero (when casting to string type float 1.0050 => "1.005" => 005 <0050)
        if (places < num) {
            return Number(f).round2(num);
        }
    
        for (var e = naturalInt.length - (places > num ? (places - num) : 0), s = 0; e >= s; e--) {
            if (naturalInt.hasOwnProperty(e)) {
                if (naturalInt[e] > 4 && naturalInt[e - 1] < 9) {
                    result[e] = 0;
                    result[e - 1] = naturalInt[e - 1] + 1;
                    break;
                } else if (naturalInt[e] > 4 && naturalInt[e - 1] === 9) {
                    result[e] = 0;
                    result[e - 1] = 0;
                    result[e - 2] = naturalInt[e - 2] < 9 ? naturalInt[e - 2] + 1 : 0;
                } else if (e === 0 && naturalInt[e] === 9 && naturalInt[e + 1] === 9) {
                    result[e] = 0;
                    result.unshift(1);
                } else {
                    break;
                }
            }
        }
    
        if (places - num > 0) {
            resultFixedLenth = result.slice(0, -(places - num));
        } else {
            for (var i = 0, l = num - places; i < l; i++) {
                result.push(0);
            }
            resultFixedLenth = result;
        }
    
        return (parseInt(resultFixedLenth.join("")) / Math.pow(10, num)).round2(num);
    };
    this.polyfill = function() {
        if (!Array.prototype.map) {
            Array.prototype.map = function (callback, thisArg) {
                var T, A, k;
                if (this == null) { throw new TypeError(' this is null or not defined'); }
                var O = Object(this), len = O.length >>> 0;
                if (typeof callback !== 'function') { throw new TypeError(callback + ' is not a function'); }
                if (arguments.length > 1) { T = thisArg; }
    
                A = new Array(len);
                k = 0;
                while (k < len) {
                    var kValue, mappedValue;
                    if (k in O) {
                        kValue = O[k];
                        mappedValue = callback.call(T, kValue, k, O);
                        A[k] = mappedValue;
                    }
                    k++;
                }
                return A;
            };
        }
    };
    
    this.init = function () {
        self.polyfill();
        Number.prototype.toFixed10 = function (decimal) {
            return calc.toFixed10(this, decimal);
        }
        Number.prototype.round2 = function (decimal) {
            return calc.round2(this, decimal);
        }
        Number.prototype.isInt = function () {
            return (Math.round(this) == this);
        }
    }
    }, calc = new Calc(); calc.init();
    

    this works good)

提交回复
热议问题