我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
我想最多四舍五入两位小数,但只有在必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在JavaScript中执行此操作?
当前回答
避免舍入到任意位数的二进制问题的适当方法是:
function roundToDigits(number, digits) {
return Number(Math.round(Number(number + 'e' + digits)) + 'e-' + digits);
}
修复toFixed()函数的一种方法是:
Number.prototype.toFixed = (prototype => {
const toFixed = prototype.toFixed;
// noinspection JSVoidFunctionReturnValueUsed
return function (fractionDigits) {
if (!fractionDigits) {
return toFixed.call(this);
} else {
// Avoid binary rounding issues
fractionDigits = Math.floor(fractionDigits);
const n = Number(Math.round(Number(+this + 'e' + fractionDigits)) + 'e-' + fractionDigits);
return toFixed.call(n, fractionDigits);
}
};
})(Number.prototype);
其他回答
parseFloat(“1.555”).toFixed(2);//返回1.55而不是1.56。
1.55是绝对正确的结果,因为在计算机中不存在1.555的精确表示。如果读数为1.555,则四舍五入至最接近的值=1.55499999999999994(64位浮点)。将这个数字四舍五入到Fixed(2)得到1.55。
如果输入为1.55499999999999,则此处提供的所有其他功能都会给出故障结果。
解决方案:在扫描前加上数字“5”,将数字舍入(更准确地说,从0开始舍入)。仅当数字真的是浮点(有小数点)时才执行此操作。
parseFloat("1.555"+"5").toFixed(2); // Returns 1.56
它可能对你有用,
Math.round(num * 100)/100;
了解toFixed和round之间的区别。您可以查看Math.round(num)vs num.toFixed(0)和浏览器不一致性。
精确的舍入方法。来源:Mozilla
(function(){
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(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));
}
// Decimal round
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Decimal floor
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Decimal ceil
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
示例:
// Round
Math.round10(55.55, -1); // 55.6
Math.round10(55.549, -1); // 55.5
Math.round10(55, 1); // 60
Math.round10(54.9, 1); // 50
Math.round10(-55.55, -1); // -55.5
Math.round10(-55.551, -1); // -55.6
Math.round10(-55, 1); // -50
Math.round10(-55.1, 1); // -60
Math.round10(1.005, -2); // 1.01 -- compare this with Math.round(1.005*100)/100 above
// Floor
Math.floor10(55.59, -1); // 55.5
Math.floor10(59, 1); // 50
Math.floor10(-55.51, -1); // -55.6
Math.floor10(-51, 1); // -60
// Ceil
Math.ceil10(55.51, -1); // 55.6
Math.ceil10(51, 1); // 60
Math.ceil10(-55.59, -1); // -55.5
Math.ceil10(-59, 1); // -50
从我在MDN上找到的precisionRound(1.005的事件返回1而不是1.01)上提出的示例开始,我编写了一个自定义precisionRound,用于管理随机精度数,1.005返回1.01。
这是一个函数:
function precisionRound(number, precision)
{
if(precision < 0)
{
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
else
return +(Math.round(number + "e+"+precision) + "e-"+precision);
}
console.log(precisionRound(1234.5678, 1)); // output: 1234.6
console.log(precisionRound(1234.5678, -1)); // output: 1230
console.log(precisionRound(1.005, 2)); // output: 1.01
console.log(precisionRound(1.0005, 2)); // output: 1
console.log(precisionRound(1.0005, 3)); // output: 1.001
console.log(precisionRound(1.0005, 4)); // output: 1.0005
对于TypeScript:
public static precisionRound(number: number, precision: number)
{
if (precision < 0)
{
let factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
else
return +(Math.round(Number(number + "e+" + precision)) +
"e-" + precision);
}
2017只需使用本机代码.toFixed()
number = 1.2345;
number.toFixed(2) // "1.23"
如果您需要严格要求并在需要时添加数字,可以使用替换
number = 1; // "1"
number.toFixed(5).replace(/\.?0*$/g,'');