Math.round 未按预期返回整数



我正在尝试编辑一个现有的(工作,首先显示(脚本,该脚本提供两个小数,以便它只提供舍入数字 - 我的编辑显示在第二个(示例(。为了清楚起见,我想要简单的整数,即 18 而不是 18.00。

function D(r){
if (r.stat === "ok") {
for (var k in r.items) {
document.write(r.items[k].inventory)
}
}
}
function D(r){
if (r.stat === "ok") {
for (var k in r.items) {
document.write(r.items[k].inventory)
Math.round()
}
}
}

您可以根据需要尝试以下方法之一来执行此操作:

  • Math.floor((

    intvalue = Math.floor( floatvalue );

  • Math.ceil((

    intvalue = Math.ceil( floatvalue );

  • Math.round((

    intvalue = Math.round( floatvalue );

例:

value = 3.65
Math.ceil(value); // 4
Math.floor(value); // 3
Math.round(value); // 4

在您的函数中:

您应该将参数传递给方法math.round以按预期工作:

Math.round(r.items[k].inventory);

Math.round 工作得很好。将您想要的东西四舍五入放在括号()之间。

let foo = {
stat: "ok",
items: {
apple: {
inventory: 18.45
}
}
}
function D(r) {
if (r.stat === "ok") {
for (var k in r.items) {
console.log(Math.round(r.items[k].inventory))
}
}
}
D(foo);

舍入数字。

有许多方法可以将数字四舍五入,每种方法都略有不同。

到最接近的整数

通常您四舍五入到最接近的整数

Math.round(9.4) == 9; // is true
Math.round(9.6) == 10; // is true
Math.round(-9.4) == -9; // is true
Math.round(-9.6) == -10; // is true

中途点四舍五入

在你走到一半的情况下,你向无穷大四舍五入

Math.round(9.5) == 10; // is true
Math.round(-9.5) == 9; // is true

不会四舍五入到最接近的甚至

Math.round(8.5) == 9; // is true
Math.round(-7.5) == -7; // is true

中途点远离零

如果要将中点从 0 四舍五入,您可以使用

9.5.toFixed() == 10; // is true
-9.5.toFixed() == -10; // is true

请注意,结果是一个字符串,因此如果您想要一个数字,请按如下方式转换它

Number( 9.5.toFixed()) === 10; // is true
Number(-9.5.toFixed()) === -10; // is true

中途点回合至偶数

如果您希望四舍五入到最接近,甚至必须创建一个函数

const roundEven = (val) => {
if (Math.abs(val % 1) === 0.5) {
return (val = Math.round(val), val - (val % 2) * Math.sign(val));
}
return Math.round(val);
}

roundEven(9.5) === 10; // is true
roundEven(-9.5) === -10; // is true
roundEven(8.5) === 8; // is true
roundEven(-8.5) === -8; // is true

show("Math.round(9.4) === " + Math.round(9.4))
show("Math.round(9.6) === " + Math.round(9.6))
show("Math.round(-9.4) === " + Math.round(-9.4))
show("Math.round(-9.6) === " + Math.round(-9.6))
show("Math.round(9.5) === " + Math.round(9.5) )
show("Math.round(-9.5) === " + Math.round(-9.5) )
show("Math.round(8.5) === " + Math.round(8.5) )
show("Math.round(-7.5) === " + Math.round(-7.5) )
show(" 9.5.toFixed() === '" + 9.5.toFixed() + "'" )
show("-9.5.toFixed() === '" + -9.5.toFixed() + "'" )

show("Number( 9.5.toFixed()) === " + Number(9.5.toFixed()))
show("Number(-9.5.toFixed()) === " + Number(-9.5.toFixed()))
const roundEven = (val) => {
if (Math.abs(val % 1) === 0.5) {
return (val = Math.round(val), val - (val % 2) * Math.sign(val));
}
return Math.round(val);
}
show("roundEven(9.5) === " + roundEven(9.5))
show("roundEven(-9.5) === " + roundEven(-9.5))
show("roundEven(8.5) === " + roundEven(8.5))
show("roundEven(-8.5) === " + roundEven(-8.5))
show("roundEven(0.5) === " + roundEven(0.5))
show("roundEven(-0.5) === " + roundEven(-0.5))
function show(text){
const d = document.createElement("div");
d.textContent = text;
document.body.appendChild(d);
}

最新更新