Javascript中toFixed方法的改进

先看一段代码

var number = 75.05;
alert(number.toFixed(1));


结果是 75.0, 显然不是预期的 75.1,以下提供一种改进的方法


/**
 * toFixed 方法的改进 by lovebing at 2013/06/26
 * @param {int} n
 * @returns {Number.prototype.toFixed}
 */
Number.prototype.toFixed = function(n) {
    if (n > 20 || n < 0) {
        throw new RangeError('toFixed() digits argument must be between 0 and 20');
    }

    var number = this;
    
    if (isNaN(number) || number >= Math.pow(10, 21)) {
        return number.toString();
    }
    if (typeof(n) == 'undefined' || n == 0) {
        return (Math.round(number)).toString();
    }

    var result = number.toString();
    var arr = result.split('.');

    // 整数的情况
    if (arr.length < 2) {
        result += '.';
        for (var i = 0; i < n; i++) {
            result += '0';
        }
        return result;
    }

    var integer = arr[0];
    var decimal = arr[1];
    if (decimal.length == n) {
        return result;
    }
    if (decimal.length < n) {
        for (var i = 0; i < n - decimal.length; i++) {
            result += '0';
        }
        return result;
    }
    result = integer + '.' + decimal.substr(0, n);

    var last = decimal.substr(n, 1);

    // 四舍五入,转换为整数再处理,避免浮点数精度的损失
    if (parseInt(last) >= 5) {
        var x = Math.pow(10, n);
        result = Math.round((parseFloat(result) * x + 1)) / x;
        result = result.toFixed(n);
    }
    return result;

}