我有这行代码,它将我的数字四舍五入到小数点后两位。但是我得到的数字是这样的: 10.8,2.4,等等。这些不是我想要的两位小数点,那么我如何改进下面的数字呢?
Math.round(price*Math.pow(10,2))/Math.pow(10,2);我想要10.80,2.40等数字。使用jQuery对我来说没问题。
发布于 2009-11-13 10:02:17
要使用定点表示法设置数字格式,只需使用toFixed方法:
(10.8).toFixed(2); // "10.80"
var num = 2.4;
alert(num.toFixed(2)); // "2.40"请注意,toFixed()返回一个字符串。
重要:请注意,toFixed在90%的时间内不会舍入,它将返回舍入后的值,但在许多情况下,它不起作用。
例如:
2.005.toFixed(2) === "2.00"
更新:
现在,您可以使用Intl.NumberFormat构造函数。它是ECMAScript Internationalization API Specification (ECMA402)的一部分。它有pretty good browser support,甚至包括IE11,它就是fully supported in Node.js。
const formatter = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
console.log(formatter.format(2.005)); // "2.01"
console.log(formatter.format(1.345)); // "1.35"
您也可以选择使用toLocaleString方法,该方法将在内部使用Intl接口:
const format = (num, decimals) => num.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
console.log(format(2.005)); // "2.01"
console.log(format(1.345)); // "1.35"
此API还为您提供了多种格式化选项,如千位分隔符、货币符号等。
发布于 2014-01-24 10:31:09
这是一个古老的话题,但仍然是排名第一的谷歌结果和提供的解决方案共享相同的浮点小数问题。下面是我使用的(非常通用的)函数thanks to MDN
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现在,要回答OP的问题,必须键入:
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"各种示例和测试(感谢!):
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));
}
function naive(value, exp) {
if (!exp) {
return Math.round(value);
}
var pow = Math.pow(10, exp);
return Math.round(value * pow) / pow;
}
function test(val, places) {
subtest(val, places);
val = typeof val === "string" ? "-" + val : -val;
subtest(val, places);
}
function subtest(val, places) {
var placesOrZero = places || 0;
var naiveResult = naive(val, places);
var roundResult = round(val, places);
if (placesOrZero >= 0) {
naiveResult = naiveResult.toFixed(placesOrZero);
roundResult = roundResult.toFixed(placesOrZero);
} else {
naiveResult = naiveResult.toString();
roundResult = roundResult.toString();
}
$("<tr>")
.append($("<td>").text(JSON.stringify(val)))
.append($("<td>").text(placesOrZero))
.append($("<td>").text(naiveResult))
.append($("<td>").text(roundResult))
.appendTo("#results");
}
test(0.565, 2);
test(0.575, 2);
test(0.585, 2);
test(1.275, 2);
test(1.27499, 2);
test(1234.5678, -2);
test(1.2345678e+2, 2);
test("123.45");
test(10.8034, 2);
test(10.8, 2);
test(1.005, 2);
test(1.0005, 2);table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid #ddd;
}
td, th {
padding: 4px;
}
th {
font-weight: normal;
font-family: sans-serif;
}
td {
font-family: monospace;
}<table>
<thead>
<tr>
<th>Input</th>
<th>Places</th>
<th>Naive</th>
<th>Thorough</th>
</tr>
</thead>
<tbody id="results">
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
发布于 2013-02-13 02:58:47
我通常将这个添加到我的个人库中,在一些建议之后,也使用@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);
}将适用于报告的异常。
https://stackoverflow.com/questions/1726630
复制相似问题