我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
这是astorije的答案的修改版本,更好地支持负值舍入。
// https://stackoverflow.com/a/21323513/384884
// Modified answer from astorije
function round(value, precision) {
// Ensure precision exists
if (typeof precision === "undefined" || +precision === 0) {
// Just do a regular Math.round
return Math.round(value);
}
// Convert the value and precision variables both to numbers
value = +value;
precision = +precision;
// Ensure the value is a number and that precision is usable
if (isNaN(value) || !(typeof precision === "number" && precision % 1 === 0)) {
// Return NaN
return NaN;
}
// Get the sign of value
var signValue = Math.sign(value);
// Get the absolute value of value
value = Math.abs(value);
// Shift
value = value.toString().split("e");
value = Math.round(+(value[0] + "e" + (value[1] ? (+value[1] + precision) : precision)));
// Shift back
value = value.toString().split("e");
value = +(value[0] + "e" + (value[1] ? (+value[1] - precision) : -precision));
// Apply the sign
value = value * signValue;
// Return rounded value
return value;
}
其他回答
这是我解决这个问题的方法:
function roundNumber(number, precision = 0) {
var num = number.toString().replace(",", "");
var integer, decimal, significantDigit;
if (num.indexOf(".") > 0 && num.substring(num.indexOf(".") + 1).length > precision && precision > 0) {
integer = parseInt(num).toString();
decimal = num.substring(num.indexOf(".") + 1);
significantDigit = Number(decimal.substr(precision, 1));
if (significantDigit >= 5) {
decimal = (Number(decimal.substr(0, precision)) + 1).toString();
return integer + "." + decimal;
} else {
decimal = (Number(decimal.substr(0, precision)) + 1).toString();
return integer + "." + decimal;
}
}
else if (num.indexOf(".") > 0) {
integer = parseInt(num).toString();
decimal = num.substring(num.indexOf(".") + 1);
significantDigit = num.substring(num.length - 1, 1);
if (significantDigit >= 5) {
decimal = (Number(decimal) + 1).toString();
return integer + "." + decimal;
} else {
return integer + "." + decimal;
}
}
return number;
}
所有浏览器和精度的通用答案:
function round(num, places) {
if(!places) {
return Math.round(num);
}
var val = Math.pow(10, places);
return Math.round(num * val) / val;
}
round(num, 2);
使用Math.rround():
Math.round(num * 100) / 100
或者更具体地说,为了确保1.005这样的数字正确,请使用Number.EPSILON:
Math.round((num + Number.EPSILON) * 100) / 100
MarkG的答案是正确的。这里是任何小数位数的通用扩展。
Number.prototype.round = function(places) {
return +(Math.round(this + "e+" + places) + "e-" + places);
}
用法:
var n = 1.7777;
n.round(2); // 1.78
单元测试:
it.only('should round floats to 2 places', function() {
var cases = [
{ n: 10, e: 10, p:2 },
{ n: 1.7777, e: 1.78, p:2 },
{ n: 1.005, e: 1.01, p:2 },
{ n: 1.005, e: 1, p:0 },
{ n: 1.77777, e: 1.8, p:1 }
]
cases.forEach(function(testCase) {
var r = testCase.n.round(testCase.p);
assert.equal(r, testCase.e, 'didn\'t get right number');
});
})
这可能会帮助您:
var result = Math.round(input*100)/100;
有关更多信息,您可以查看Math.round(num)vs num.toFixed(0)和浏览器不一致性