Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数学圆不起作用——其他答案没有帮助_Javascript - Fatal编程技术网

Javascript 数学圆不起作用——其他答案没有帮助

Javascript 数学圆不起作用——其他答案没有帮助,javascript,Javascript,试图编辑一个现有的(工作的,首先显示的)脚本,它提供两个小数,这样它将只提供四舍五入的数字-我的编辑显示在第二个(示例)。为了清楚起见,我想要简单的整数,即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){

试图编辑一个现有的(工作的,首先显示的)脚本,它提供两个小数,这样它将只提供四舍五入的数字-我的编辑显示在第二个(示例)。为了清楚起见,我想要简单的整数,即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()
        }
     }
}

数学圆很好用。把你想要取整的东西放在括号之间
()

让foo={
统计:“好的”,
项目:{
苹果公司:{
库存:18.45
}
}
}
功能D(r){
如果(r.stat==“正常”){
对于(r项中的变量k){
console.log(Math.round(r.items[k].inventory))
}
}
}

D(foo)Math.round工作正常。把你想要取整的东西放在括号之间
()

让foo={
统计:“好的”,
项目:{
苹果公司:{
库存:18.45
}
}
}
功能D(r){
如果(r.stat==“正常”){
对于(r项中的变量k){
console.log(Math.round(r.items[k].inventory))
}
}
}

D(foo)您可以根据需要尝试以下方法之一:

  • 数学地板()

    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);

您可以根据自己的要求尝试以下方法之一:

  • 数学地板()

    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(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(“数学圆(9.4)==”+数学圆(9.4))
显示(“数学圆(9.6)=”+数学圆(9.6))
显示(“数学圆(-9.4)==”+数学圆(-9.4))
显示(“数学圆(-9.6)==”+数学圆(-9.6))
显示(“数学圆(9.5)=”+数学圆(9.5))
显示(“数学圆(-9.5)==”+数学圆(-9.5))
显示(“数学圆(8.5)=”+数学圆(8.5))
显示(“数学圆(-7.5)==”+数学圆(-7.5))
显示(“9.5.toFixed()==”+9.5.toFixed()+”)
显示(“-9.5.toFixed()==”+-9.5.toFixed()+”)
显示(“数字(9.5.toFixed())==”+数字(9.5.toFixed()))
显示(“数字(-9.5.toFixed())==”+数字(-9.5.toFixed()))
const round偶数=(val)=>{
如果(数学绝对值(值%1)==0.5){
返回值(val=Math.round(val),val-(val%2)*数学符号(val));
}
返回Math.round(val);
}
显示(“整数(9.5)==”+整数(9.5))
显示(“整数倍(-9.5)==”+整数倍(-9.5))
显示(“整数倍(8.5)==”+整数倍(8.5))
显示(“整数倍(-8.5)==”+整数倍(-8.5))
显示(“整数倍(0.5)==”+整数倍(0.5))
显示(“整数倍(-0.5)==”+整数倍(-0.5))
功能显示(文本){
const d=document.createElement(“div”);
d、 text内容=文本;
文件.正文.附件(d);
}
四舍五入数字。 有很多种方法可以对一个数字进行四舍五入,每种方法都略有不同

最接近整数 一般来说,四舍五入到最接近的整数

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(“数学圆(9.4)==”+数学圆(9.4))
显示(“数学圆(9.6)=”+数学圆(9.6))
显示(“数学圆(-9.4)==”+数学圆(-9.4))
显示(“数学圆(-9.6)==”+数学圆(-9.6))
显示(“数学圆(9.5)=”+数学圆(9.5))
显示(“数学圆(-9.5)==”+数学圆(-9.5))
显示(“数学圆(8.5)=”+数学圆(8.5))
显示(“数学圆(-7.5)==”+数学圆(-7.5))
显示(“9.5.toFixed()==”+9.5.toFixed()+”)
显示(“-9.5.toFixed()==”+-9.5.toFixed()+”)
显示(“数字(9.5.toFixed())==”+数字(9.5.toFixed()))
显示(“数字(-9.5.toFixed())==”+数字(-9.5.toFixed()))
const round偶数=(val)=>{
如果(数学绝对值(值%1)==0.5){
返回值(val=Math.round(val),val-(val%2)*数学符号(val));
}
返回Math.round(val);
}
显示(“整数(9.5)==”+整数(9.5))
显示(“整数倍(-9.5)==”+整数倍(-9.5))
显示(“整数倍(8.5)==”+整数倍(8.5))
显示(“整数倍(-8.5)==”+整数倍(-8.5))
显示(“整数倍(0.5)==”+整数倍(0.5))
显示(“整数倍(-0.5)==”+整数倍(-0.5))
功能显示(文本){
const d=document.createElement(“div”);
d、 text内容=文本;
文件.正文.附件(d);

}
调用不带参数的
Math.round
时,您希望得到什么结果?为什么只将
Math.round()
放入其中而不带参数且不在任何地方返回结果会有任何效果?也许你应该阅读你试图使用的任何方法,看看例子