最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Math.round not returning integers as expected - Stack Overflow

programmeradmin2浏览0评论

I'm trying to edit an existing (working, shown first) script that delivers two decimals so that it will only deliver rounded numbers - my edit shown second (example). To be clear I want simple integers i.e. 18 not 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()
        }
     }
}

I'm trying to edit an existing (working, shown first) script that delivers two decimals so that it will only deliver rounded numbers - my edit shown second (example). To be clear I want simple integers i.e. 18 not 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()
        }
     }
}
Share Improve this question edited Sep 30, 2022 at 20:13 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Jul 26, 2017 at 22:31 jSchmitzjSchmitz 11 silver badge1 bronze badge 2
  • 4 What result were you expecting from calling Math.round with no arguments? – wbadart Commented Jul 26, 2017 at 22:34
  • 5 Why would just throwing Math.round() in there with no arguments and without returning the result anywhere do anything at all? Maybe you should just read the docs for whatever method you're trying to use, and look at the examples. – adeneo Commented Jul 26, 2017 at 22:34
Add a ment  | 

3 Answers 3

Reset to default 5

You can try one of these methods to do that according to your requirements:

  • Math.floor()

    intvalue = Math.floor( floatvalue );

  • Math.ceil()

    intvalue = Math.ceil( floatvalue );

  • Math.round()

    intvalue = Math.round( floatvalue );

Example:

value = 3.65

Math.ceil(value); // 4

Math.floor(value); // 3

Math.round(value); // 4

in your function :

you should pass argument to method math.round to work as expected :

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

Math.round works just fine. Put the thing you want rounded in between the parentheses ().

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

Rounding numbers.

There are many ways to round a number each with slight differences.

To closest integer

Generally you round to the closest whole number

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

Midway point round up

In the case where you are halfway you round up towards Infinity

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

You do not round to the nearest even

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

Midway point round away from zero

If you want to round the mid point away from 0 you can use

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

Note the result is a string so if you want a number convert it as follows

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

Midway point round to even

If you wish to round to the nearest even you will have to create a function

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

Example

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

发布评论

评论列表(0)

  1. 暂无评论