在JavaScript中使用正好两个小数格式化数字

在JavaScript中使用正好两个小数格式化数字,javascript,rounding,decimal-point,Javascript,Rounding,Decimal Point,我有一行代码,它把我的数字四舍五入到小数点后两位。但是我得到的数字是这样的:10.8,2.4,等等。这些不是我的想法,小数点后两位,那么我如何改进下面的内容呢 Math.round(price*Math.pow(10,2))/Math.pow(10,2); 我想要10.80、2.40等数字。我可以使用jQuery。要使用定点表示法格式化数字,只需使用以下方法: 请注意,toFixed()返回一个字符串 重要信息:请注意,toFixed在90%的时间内不进行舍入,它将返回舍入值,但在许多情况下,

我有一行代码,它把我的数字四舍五入到小数点后两位。但是我得到的数字是这样的:10.8,2.4,等等。这些不是我的想法,小数点后两位,那么我如何改进下面的内容呢

Math.round(price*Math.pow(10,2))/Math.pow(10,2);

我想要10.80、2.40等数字。我可以使用jQuery。

要使用定点表示法格式化数字,只需使用以下方法:

请注意,
toFixed()
返回一个字符串

重要信息:请注意,toFixed在90%的时间内不进行舍入,它将返回舍入值,但在许多情况下,它不起作用

例如:

2.005.toFixed(2)=“2.00”

更新: 现在,您可以使用构造函数。它是(ECMA402)的一部分。它有,甚至包括IE11,它是

const formatter=new Intl.NumberFormat('en-US'{
最小分数位数:2,
最大分数位数:2,
});
console.log(formatter.format(2.005));//"2.01"
console.log(formatter.format(1.345));//“1.35”
toFixed(n)提供小数点后的n个长度;toPrecision(x) 提供x总长度

使用下面的方法

// Example: toPrecision(4) when the number has 7 digits (3 before, 4 after)
    // It will round to the tenths place
    num = 500.2349;
    result = num.toPrecision(4); // result will equal 500.2
如果你想固定电话号码,请使用

result = num.toFixed(2);

@heridev和我在jQuery中创建了一个小函数

您可以尝试下一步:

HTML

​ 在线演示:


我通常将其添加到我的个人库中,在提出一些建议后,也使用@TimiNeuton解决方案,并使其适用于十进制长度,此解决方案最适合:

function precise_round(num, decimals) {
   var t = Math.pow(10, decimals);   
   return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
}

将适用于报告的异常。

我没有找到此问题的准确解决方案,因此我创建了自己的:

function inprecise_round(value, decPlaces) {
  return Math.round(value*Math.pow(10,decPlaces))/Math.pow(10,decPlaces);
}

function precise_round(value, decPlaces){
    var val = value * Math.pow(10, decPlaces);
    var fraction = (Math.round((val-parseInt(val))*10)/10);

    //this line is for consistency with .NET Decimal.Round behavior
    // -342.055 => -342.06
    if(fraction == -0.5) fraction = -0.6;

    val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces);
    return val;
}
示例:

精确循环中的函数(值,小数点){ 返回Math.round(值*Math.pow(10个小数点))/Math.pow(10个小数点); } 函数精确四舍五入(值,小数点){ var val=数值*数学功率(10个小数点); var分数=(数学四舍五入((val-parseInt(val))*10)/10); //此行用于与.NET Decimal.Round行为保持一致 // -342.055 => -342.06 如果(分数==-0.5)分数=-0.6; val=Math.round(parseInt(val)+分数)/Math.pow(10,deplaces); 返回val; } //这可能会根据浏览器环境产生不同的结果 console.log(“342.055.toFixed(2):”,342.055.toFixed(2));//铬合金和IE10上的342.06 console.log(“inprecise_-round(342.055,2):”,inprecise_-round(342.055,2));//342.05 console.log(“精确(342.055,2):”,精确(342.055,2));//342.06 console.log(“精确的圆球(-342.055,2):”,精确的圆球(-342.055,2));//-342.06 log(“inprecise_round(0.565,2):”,inprecise_round(0.565,2));//0.56
console.log(“精确(0.565,2):”,精确(0.565,2));//0.57我不知道为什么我不能在之前的答案中添加注释(我不知道,也许我是无望的盲人),但我用@Miguel的答案想出了一个解决方案:

function precise_round(num,decimals) {
   return Math.round(num*Math.pow(10, decimals)) / Math.pow(10, decimals);
}
以及它的两条评论(来自@bighostkim和@Imre):

  • 精确(1.275,2)
    不返回1.28的问题
  • precision\u round(6,2)
    无法返回6.00(如他所愿)的问题
我的最终解决办法如下:

function precise_round(num,decimals) {
    var sign = num >= 0 ? 1 : -1;
    return (Math.round((num*Math.pow(10,decimals)) + (sign*0.001)) / Math.pow(10,decimals)).toFixed(decimals);
}
正如您所看到的,我必须添加一点“更正”(它不是什么,但因为Math.round是有损的-您可以在jsfiddle.net上检查它-这是我知道如何“修复”它的唯一方法)。它在已经填充的数字上加0.001,因此在十进制值的右边加一个
1
3个
0
s。所以使用起来应该是安全的

之后,我添加了
.toFixed(decimal)
,以便始终以正确的格式输出数字(小数量正确)

差不多就是这样。好好利用它;)


编辑:为负数的“更正”添加了功能。

浮点值的问题在于,它们试图用固定的位表示无限量的(连续)值。所以很自然,游戏中肯定会有一些损失,你会被一些价值观所侵蚀

当计算机将1.275存储为浮点值时,它实际上不会记住它是1.275还是1.27499999999993,甚至是1.27500000000000002。在四舍五入到两位小数后,这些值应该给出不同的结果,但它们不会,因为对于计算机来说,它们在存储为浮点值后看起来完全相同,并且无法恢复丢失的数据。任何进一步的计算只会累积这种不精确性

因此,如果精度很重要,您必须从一开始就避免使用浮点值。最简单的选择是

  • 使用
  • 使用字符串存储和传递值(伴随字符串操作)
  • 使用整数(例如,您可以传递实际值的百分之一百,例如,以美分表示的金额,而不是以美元表示的金额)
例如,当使用整数存储百分位数时,查找实际值的函数非常简单:

function descale(num, decimals) {
    var hasMinus = num < 0;
    var numString = Math.abs(num).toString();
    var precedingZeroes = '';
    for (var i = numString.length; i <= decimals; i++) {
        precedingZeroes += '0';
    }
    numString = precedingZeroes + numString;
    return (hasMinus ? '-' : '') 
        + numString.substr(0, numString.length-decimals) 
        + '.' 
        + numString.substr(numString.length-decimals);
}

alert(descale(127, 2));
函数除垢(数值,小数){
var hasMinus=num<0;
var numString=Math.abs(num.toString();
var前序零=“”;
对于(var i=numString.length;i 0&&parts[0]。长度>0&&parts[0]。字符(0)=='-';
var integralPart=parts.length==0?'0':(hasMinus?parts[0]。substr(1):parts[0]);
var decimalPart=零件长度>1?零件[1]:'';
if(小数部分长度>小数){
变量舍入数=小数部分字符(小数);
小数部分=小数部分substr(0,小数);
如果('56789'.indexOf(舍入编号)>-1){
变量数=整数部分+小数部分;
var i=数字。长度;
var trailingZeroes='';
var JustOne和TrailingZeroes=真;
做{
我--;
var roundedNumber='1234567890'.charAt(parseInt(numbers.charAt(i));
function precise_round(num,decimals) {
   return Math.round(num*Math.pow(10, decimals)) / Math.pow(10, decimals);
}
function precise_round(num,decimals) {
    var sign = num >= 0 ? 1 : -1;
    return (Math.round((num*Math.pow(10,decimals)) + (sign*0.001)) / Math.pow(10,decimals)).toFixed(decimals);
}
function descale(num, decimals) {
    var hasMinus = num < 0;
    var numString = Math.abs(num).toString();
    var precedingZeroes = '';
    for (var i = numString.length; i <= decimals; i++) {
        precedingZeroes += '0';
    }
    numString = precedingZeroes + numString;
    return (hasMinus ? '-' : '') 
        + numString.substr(0, numString.length-decimals) 
        + '.' 
        + numString.substr(numString.length-decimals);
}

alert(descale(127, 2));
function precise_round(num, decimals) {
    var parts = num.split('.');
    var hasMinus = parts.length > 0 && parts[0].length > 0 && parts[0].charAt(0) == '-';
    var integralPart = parts.length == 0 ? '0' : (hasMinus ? parts[0].substr(1) : parts[0]);
    var decimalPart = parts.length > 1 ? parts[1] : '';
    if (decimalPart.length > decimals) {
        var roundOffNumber = decimalPart.charAt(decimals);
        decimalPart = decimalPart.substr(0, decimals);
        if ('56789'.indexOf(roundOffNumber) > -1) {
            var numbers = integralPart + decimalPart;
            var i = numbers.length;
            var trailingZeroes = '';
            var justOneAndTrailingZeroes = true;
            do {
                i--;
                var roundedNumber = '1234567890'.charAt(parseInt(numbers.charAt(i)));
                if (roundedNumber === '0') {
                    trailingZeroes += '0';
                } else {
                    numbers = numbers.substr(0, i) + roundedNumber + trailingZeroes;
                    justOneAndTrailingZeroes = false;
                    break;
                }
            } while (i > 0);
            if (justOneAndTrailingZeroes) {
                numbers = '1' + trailingZeroes;
            }
            integralPart = numbers.substr(0, numbers.length - decimals);
            decimalPart = numbers.substr(numbers.length - decimals);
        }
    } else {
        for (var i = decimalPart.length; i < decimals; i++) {
            decimalPart += '0';
        }
    }
    return (hasMinus ? '-' : '') + integralPart + (decimals > 0 ? '.' + decimalPart : '');
}

alert(precise_round('1.275', 2));
alert(precise_round('1.27499999999999993', 2));
function roundFloat(num,dec){
    var d = 1;
    for (var i=0; i<dec; i++){
        d += "0";
    }
    return Math.round(num * d) / d;
}
(Math.round(num*100)/100).toFixed(2)
(Math.round((num * 1000)/10)/100).toFixed(2)
function round(value, exp) {
  if (typeof exp === 'undefined' || +exp === 0)
    return Math.round(value);

  value = +value;
  exp = +exp;

  if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
    return NaN;

  // Shift
  value = value.toString().split('e');
  value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));

  // Shift back
  value = value.toString().split('e');
  return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
}
round(1.275, 2);   // Returns 1.28
round(1.27499, 2); // Returns 1.27
round(1234.5678, -2);   // Returns 1200
round(1.2345678e+2, 2); // Returns 123.46
round("123.45");        // Returns 123
round(10.8034, 2).toFixed(2); // Returns "10.80"
round(10.8, 2).toFixed(2);    // Returns "10.80"
function round2Fixed(value) {
  value = +value;

  if (isNaN(value))
    return NaN;

  // Shift
  value = value.toString().split('e');
  value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2)));

  // Shift back
  value = value.toString().split('e');
  return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(2);
}
round2Fixed(10.8034); // Returns "10.80"
round2Fixed(10.8);    // Returns "10.80"
Number.prototype.getDecimals = function ( decDigCount ) {
   return this.toFixed(decDigCount);
}
var a = 56.23232323;
a.getDecimals(2); // will return 56.23
Number.prototype.getDecimals = function ( decDigCount ) {
   return ( decDigCount > 20 ) ? this : this.toFixed(decDigCount);
}
function parseDecimalRoundAndFixed(num,dec){
  var d =  Math.pow(10,dec);
  return (Math.round(num * d) / d).toFixed(dec);
}
Number(Math.round(1.005+'e2')+'e-2'); // 1.01
(Math.round((10.2)*100)/100).toFixed(2)
(Math.round((.05)*100)/100).toFixed(2)
(Math.round((4.04)*100)/100).toFixed(2)
Number(((Math.random() * 100) + 1).toFixed(2))
function parseNumber(val, decimalPlaces) {
    if (decimalPlaces == null) decimalPlaces = 0
    var ret = Number(val).toFixed(decimalPlaces)
    return Number(ret)
}
function glbfrmt (number, decimals, seperator) {
    return typeof number !== 'number' ? number : number.toPrecision( number.toString().split(seperator)[0].length + decimals);
}
function toDec(num, dec)
{
        if(typeof dec=='undefined' || dec<0)
                dec = 2;

        var tmp = dec + 1;
        for(var i=1; i<=tmp; i++)
                num = num * 10;

        num = num / 10;
        num = Math.round(num);
        for(var i=1; i<=dec; i++)
                num = num / 10;

        num = num.toFixed(dec);

        return num;
}
function round_down(value, decPlaces) {
    return Math.floor(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
}
function round_up(value, decPlaces) {
    return Math.ceil(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
}
function round_nearest(value, decPlaces) {
    return Math.round(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
}
function round(value: number, decimals: number) {
    return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
fixPrecision: function (value) {
    var me = this,
        nan = isNaN(value),
        precision = me.decimalPrecision;

    if (nan || !value) {
        return nan ? '' : value;
    } else if (!me.allowDecimals || precision <= 0) {
        precision = 0;
    }

    //[1]
    //return parseFloat(Ext.Number.toFixed(parseFloat(value), precision));
    precision = precision || 0;
    var negMultiplier = value < 0 ? -1 : 1;

    //[2]
    var numWithExp = parseFloat(value + "e" + precision);
    var roundedNum = parseFloat(Math.round(Math.abs(numWithExp)) + 'e-' + precision) * negMultiplier;
    return parseFloat(roundedNum.toFixed(precision));
},
9.995 * 100 = 999.4999999999999
Whereas 9.995e2 = 999.5
This discrepancy causes Math.round(9.995 * 100) = 999 instead of 1000.
Use e notation instead of multiplying /dividing by Math.Pow(10,precision).
td[row].innerHTML = price.toPrecision(price.toFixed(decimals).length
const roundNumberToTwoDecimalPlaces = (num) => Number(new Intl.NumberFormat('en-US', {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
}).format(num));

roundNumberToTwoDecimalPlaces(1.344); // => 1.34
roundNumberToTwoDecimalPlaces(1.345); // => 1.35