我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
使用类似于以下的方法进行汇总:
num = 519.805;
dp = Math.pow(10, 2);
num = parseFloat(num.toString().concat("1"));
rounded = Math.round((num + Number.EPSILON)* dp)/dp;
因为它会处理在最后只有一个小数点四舍五入的数字。
其他回答
只有在必要的时候你说?
如果你也关心负数,我建议你。。。
有些答案对负数不太有效。。。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>roundPrecision</title>
<script>
class MyMath{
static roundPrecision(number, precision, fillZeros) {
// Number you want to round
// precision nb of decimals
// fillZeros the number of 0 You want to add IF necessary!
// 0 = no fill with zeros.
let num = number;
let prec = precision;
let exp = Math.pow(10, prec);
let round = Math.round(number * exp)/exp
if (fillZeros>0) {
return round.toFixed(fillZeros)
}
return round;
}
}
</script>
</head>
<body>
<p class="myMath" id="field1"></p>
<p class="myMath" id="field2"></p>
<p class="myMath" id="field3"></p>
<p class="myMath" id="field4"></p>
<p class="myMath" id="field5"></p>
<p class="myMath" id="field6"></p>
<p class="myMath" id="field7"></p>
<script>
document.getElementById("field1").innerHTML = MyMath.roundPrecision(5, 0, 3); // 5.000
document.getElementById("field2").innerHTML = MyMath.roundPrecision(Math.PI, 2, 4); // 3.1400
document.getElementById("field3").innerHTML = MyMath.roundPrecision(2.4, 1, 2); // 2.40
document.getElementById("field4").innerHTML = MyMath.roundPrecision(2.9, 0, 2); // 3.00
document.getElementById("field5").innerHTML = MyMath.roundPrecision(10, 0, 2); // 10.00
document.getElementById("field6").innerHTML = MyMath.roundPrecision(-10.5, 1, 2); // 10.00
document.getElementById("field7").innerHTML = MyMath.roundPrecision(-1.006, 2, 0); // 10.00
</script>
</body>
</html>
尝试使用jQuery.number插件:
var number = 19.8000000007;
var res = 1 * $.number(number, 2);
+(10).toFixed(2); // = 10
+(10.12345).toFixed(2); // = 10.12
(10).toFixed(2); // = 10.00
(10.12345).toFixed(2); // = 10.12
我尝试了自己的代码。试试看:
function AmountDispalyFormat(value) {
value = value.toFixed(3);
var amount = value.toString().split('.');
var result = 0;
if (amount.length > 1) {
var secondValue = parseInt(amount[1].toString().slice(0, 2));
if (amount[1].toString().length > 2) {
if (parseInt(amount[1].toString().slice(2, 3)) > 4) {
secondValue++;
if (secondValue == 100) {
amount[0] = parseInt(amount[0]) + 1;
secondValue = 0;
}
}
}
if (secondValue.toString().length == 1) {
secondValue = "0" + secondValue;
}
result = parseFloat(amount[0] + "." + secondValue);
} else {
result = parseFloat(amount);
}
return result;
}
我只是想根据前面提到的答案分享我的方法:
让我们创建一个函数,将任何给定的数值舍入到给定的小数位数:
function roundWDecimals(n, decimals) {
if (!isNaN(parseFloat(n)) && isFinite(n)) {
if (typeof(decimals) == typeof(undefined)) {
decimals = 0;
}
var decimalPower = Math.pow(10, decimals);
return Math.round(parseFloat(n) * decimalPower) / decimalPower;
}
return NaN;
}
并为数字原型引入一种新的“舍入”方法:
Object.defineProperty(Number.prototype, 'round', {
enumerable: false,
value: function(decimals) {
return roundWDecimals(this, decimals);
}
});
您可以测试它:
函数舍入WDecimals(n,小数){if(!isNaN(parseFloat(n))&&isFinite(n){if(typeof(小数)==typeof(未定义)){小数=0;}var decimalPower=数学.pow(10,小数);return Math.round(parseFloat(n)*decimalPower)/decimalPower;}返回NaN;}Object.defineProperty(Number.prototype,'round'{可枚举:false,值:函数(小数){返回舍入WDecimals(this,小数);}});var舍入=[{num:10,小数:2},{num:1.7777777,小数:2},{num:9.1,小数:2},{num:55.55,小数:1},{num:55.549,小数:1},{num:55,小数:0},{num:54.9,小数:0},{num:-55.55,小数:1},{num:-55.551,小数:1},{num:-55,小数:0},{num:1.005,小数:2},{num:1.005,小数:2},{num:198000000007,小数:2},],table='<table border=“1”><tr><th>Num</th><th>小数</th><th>结果</th></tr>';$.each(roundables,function(){表+='<tr>'+“<td>”+此.num+“</td>”+“<td>”+此小数+“</td>”+'<td>'+this.num.round(this.decimals)+'</td>'+'</tr>';});table+=“</table>”;$('.results').append(表);<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js“></script><div class=“results”></div>