# JavaScript数学，四舍五入到小数点后两位[重复]

2020/09/27 18:41 · javascript ·  · 0评论

``````var discount = Math.round(100 - (price / listprice) * 100);
``````

``````var discount = (price / listprice).toFixed(2);
``````

toFixed将为您舍入或舍入，具体取决于小数点后2位的值。

``````var discount = +((price / listprice).toFixed(2));
``````

``````   var discount = roundTo((price / listprice), 2);

function roundTo(n, digits) {
if (digits === undefined) {
digits = 0;
}

var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
var test =(Math.round(n) / multiplicator);
return +(test.toFixed(digits));
}
``````

``````function roundTo(n, digits) {
var negative = false;
if (digits === undefined) {
digits = 0;
}
if( n < 0) {
negative = true;
n = n * -1;
}
var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
n = (Math.round(n) / multiplicator).toFixed(digits);
if( negative ) {
n = (n * -1).toFixed(digits);
}
return n;
}
``````

``````Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2');
``````

``````Number(Math.round(100 - (price / listprice) * 100 + 'e2') + 'e-2').toFixed(2);
``````

``````function round(value, decimals) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
``````

``````Number.prototype.round = function(decimals) {
return Number((Math.round(this + "e" + decimals)  + "e-" + decimals));
}
``````

``````var numberToRound = 100 - (price / listprice) * 100;
numberToRound.round(2);
numberToRound.round(2).toFixed(2); //Converts it to string with two decimals
``````

``````var discount = Math.round((100 - (price / listprice) * 100) * 100) / 100;
``````

``````function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
round(1.005, 2); // 1.01
``````

``````function roundToXDigits(value: number, digits: number) {
value = value * Math.pow(10, digits);
value = Math.round(value);
value = value / Math.pow(10, digits);
return value;
}
``````

``````function roundToXDigits(value, digits) {
if(!digits){
digits = 2;
}
value = value * Math.pow(10, digits);
value = Math.round(value);
value = value / Math.pow(10, digits);
return value;
}
``````

`toFixed(2)`返回一个字符串，您将始终获得两个小数位。这些可能为零。如果您想抑制最终的零，只需执行以下操作：

``````var discount = + ((price / listprice).toFixed(2));
``````

``````var discount = Math.round(price / listprice * 100) / 100;
``````

OP始终希望保留两位小数，但是如果toFixed（）在Firefox中损坏，则需要先修复。

https://bugzilla.mozilla.org/show_bug.cgi?id=1134388

## 两个小数

``````x      = .123456
result = Math.round(x * 100) / 100  // result .12
``````

## 三小数

``````x      = .123456
result = Math.round(x * 1000) / 1000      // result .123
``````
``````    function round(num,dec)
{
num = Math.round(num+'e'+dec)
return Number(num+'e-'+dec)
}
//Round to a decimal of your choosing:
round(1.3453,2)
``````

``````

var testNum = 134.9567654;
var decPl = 2;
var testRes = roundDec(testNum,decPl);
alert (testNum + ' rounded to ' + decPl + ' decimal places is ' + testRes);

function roundDec(nbr,dec_places){
var mult = Math.pow(10,dec_places);
return Math.round(nbr * mult) / mult;
}

``````
``````function discoverOriginalPrice(discountedPrice, salePercentage) {
var originalPrice = discountedPrice / (1 - (salePercentage * .01));
return +originalPrice.toFixed(2);
}``````

``````var value=200.2365455;
result=Math.round(value*100)/100    //result will be 200.24
``````