我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
您也可以重写Math.round函数来进行舍入校正,并为小数添加一个参数,并使用它,如:Math.round(Number,decimals)。请记住,这将覆盖内置组件Math.round,并为其提供另一个属性,而不是原始属性。
var round = Math.round;
Math.round = function (value, decimals) {
decimals = decimals || 0;
return Number(round(value + 'e' + decimals) + 'e-' + decimals);
}
然后你可以这样简单地使用它:
Math.round(1.005, 2);
https://jsfiddle.net/k5tpq3pd/3/
其他回答
number=(parseInt((number +0.005)*100))/100;
如果要正常舍入(2位小数),请添加0.005
8.123 +0.005=> 8.128*100=>812/100=>8.12
8.126 +0.005=> 8.131*100=>813/100=>8.13
我只是想根据前面提到的答案分享我的方法:
让我们创建一个函数,将任何给定的数值舍入到给定的小数位数:
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>
所有浏览器和精度的通用答案:
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);
更简单的ES6方法是
const round = (x, n) =>
Number(parseFloat(Math.round(x * Math.pow(10, n)) / Math.pow(10, n)).toFixed(n));
此模式还返回要求的精度。
ex:
round(44.7826456, 4) // yields 44.7826
round(78.12, 4) // yields 78.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;
}