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

JavaScript

逆天樱

2020-03-12

我有这行代码将我的数字四舍五入到小数点后两位。但是我得到这样的数字:10.8、2.4等。这些不是我对小数点后两位的想法,因此我如何改善以下内容?

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

我想要数字10.80、2.40等。使用jQuery对我来说很好。

第987篇《在JavaScript中使用正好两位小数格式化数字》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

12个回答
前端逆天 2020.03.12

Simple as this.

var rounded_value=Math.round(value * 100) / 100;
达蒙西里 2020.03.12

I found a very simple way that solved this problem for me and can be used or adapted:

td[row].innerHTML = price.toPrecision(price.toFixed(decimals).length
宝儿理查德村村 2020.03.12
(Math.round((10.2)*100)/100).toFixed(2)

That should yield: 10.20

(Math.round((.05)*100)/100).toFixed(2)

That should yield: 0.05

(Math.round((4.04)*100)/100).toFixed(2)

That should yield: 4.04

etc.

西门老丝Pro 2020.03.12
Number(((Math.random() * 100) + 1).toFixed(2))

this will return a random number from 1 to 100 rounded to 2 decimal places.

Itachi梅Harry 2020.03.12

Put the following in some global scope:

Number.prototype.getDecimals = function ( decDigCount ) {
   return this.toFixed(decDigCount);
}

and then try:

var a = 56.23232323;
a.getDecimals(2); // will return 56.23

Update

Note that toFixed() can only work for the number of decimals between 0-20 i.e. a.getDecimals(25) may generate a javascript error, so to accomodate that you may add some additional check i.e.

Number.prototype.getDecimals = function ( decDigCount ) {
   return ( decDigCount > 20 ) ? this : this.toFixed(decDigCount);
}
西门斯丁 2020.03.12

四舍五入

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);
}

合并https://stackoverflow.com/a/7641824/1889449https://www.kirupa.com/html5/rounding_numbers_in_javascript.htm谢谢他们。

米亚小哥斯丁 2020.03.12

四舍五入您的十进制值,然后将其toFixed(x)用作您的期望数字。

function parseDecimalRoundAndFixed(num,dec){
  var d =  Math.pow(10,dec);
  return (Math.round(num * d) / d).toFixed(dec);
}

呼叫

parseDecimalRoundAndFixed(10.800243929,4)=> 10.80 parseDecimalRoundAndFixed(10.807243929,2)=> 10.81

达蒙小胖 2020.03.12

这是一个简单的

function roundFloat(num,dec){
    var d = 1;
    for (var i=0; i<dec; i++){
        d += "0";
    }
    return Math.round(num * d) / d;
}

使用方式 alert(roundFloat(1.79209243929,4));

杰斯菲德尔

伽罗Tom猪猪 2020.03.12

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

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;
}

例子:

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;
}

// This may produce different results depending on the browser environment
console.log("342.055.toFixed(2)         :", 342.055.toFixed(2)); // 342.06 on Chrome & IE10

console.log("inprecise_round(342.055, 2):", inprecise_round(342.055, 2)); // 342.05
console.log("precise_round(342.055, 2)  :", precise_round(342.055, 2));   // 342.06
console.log("precise_round(-342.055, 2) :", precise_round(-342.055, 2));  // -342.06

console.log("inprecise_round(0.565, 2)  :", inprecise_round(0.565, 2));   // 0.56
console.log("precise_round(0.565, 2)    :", precise_round(0.565, 2));     // 0.57

神无Sam乐 2020.03.12

一种获得100%确定带有2个小数的数字的方法:

(Math.round(num*100)/100).toFixed(2)

如果这导致舍入错误,则可以使用James在其评论中解释的以下内容:

(Math.round((num * 1000)/10)/100).toFixed(2)
樱理查德 2020.03.12

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);
逆天卡卡西 2020.03.12

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

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);
}

将适用于所报告的异常。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android