Math.round()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
Math.round() 颿°ã¯ã弿°ã¨ãã¦ä¸ããæ°ã忍äºå
¥ãã¦ããã£ã¨ãè¿ä¼¼ã®æ´æ°ãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
console.log(Math.round(0.9));
// äºæ³ãããçµæ: 1
console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05));
// äºæ³ãããçµæ: 6 6 5
console.log(Math.round(-5.05), Math.round(-5.5), Math.round(-5.95));
// äºæ³ãããçµæ: -5 -5 -6
æ§æ
Math.round(x)
弿°
x-
æ°å¤ã§ãã
è¿å¤
x ã®å¤ããã£ã¨ãè¿ä¼¼ã®æ´æ°ã«åæ¨äºå
¥ããå¤ã
解説
弿°ã®å°æ°é¨åã 0.5 以ä¸ã®å ´åããã®å¼æ°ã¯ã次ã«å¤§ããæ´æ°ã«åãä¸ãããã¾ãã弿°ã®å°æ°é¨åã 0.5 æªæºã®å ´åããã®å¼æ°ã¯ã次ã«å°ããæ´æ°ã«åãä¸ãããã¾ããå°æ°é¨åã 0.5 ã§ããå ´åã¯ãæ£ã®ç¡éå¤§ã®æ¹åã§æ¬¡ã®æ´æ°ã«ä¸¸ãããã¾ããããã¯å¤ãã®è¨èªã® round() 颿°ã¨ç°ãªããã¨ã«æ³¨æãã¦ãã ããããã®å ´åã¯ããã¦ãã0 ããé ãããæ¬¡ã®æ´æ°ã«ä¸¸ãããã¾ã (å°æ°é¨åã 0.5 ã§ããè² ã®å¤ã忍äºå
¥ããå ´åã«ãçµæãå¤ããã¾ã)ã
ã¡ã¢:
ããã¯ãå¤ãã®è¨èªã® round() 颿°ã¨ã¯ç°ãªãã¾ãããããã®é¢æ°ã¯ãåå¢åãã¼ãããé¢ããæ¹åã¸ä¸¸ãããã¨ãå¤ããå°æ°é¨åãã¡ããã© 0.5 ã®è² æ°ã®å ´åã¯ç°ãªãçµæã¨ãªãã¾ãã
Math.round(x) ã¯ã Math.floor(x + 0.5) ã¨ã¾ã£ããåãã§ã¯ããã¾ããã x ã -0ãã¾ã㯠-0.5 ⤠x < 0 ã®å ´åã Math.round(x) 㯠-0 ãè¿ãã䏿¹ã Math.floor(x + 0.5) 㯠0 ãè¿ãã¾ãããããããã®éããæ½å¨çãªç²¾åº¦ã¨ã©ã¼ãç¡è¦ããã°ã Math.round(x) 㨠Math.floor(x + 0.5) ã¯ä¸è¬çã«åçã§ãã
round() 㯠Math ãªãã¸ã§ã¯ãã®éçãªã¡ã½ãããªã®ã§ãèªãçæãã Math ãªãã¸ã§ã¯ãã®ã¡ã½ããã¨ãã¦ã§ã¯ãªãã常ã«ãMath.round() ã¨ãã¦ä½¿ç¨ããããã«ãã¦ãã ãã (Math ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã¯ããã¾ãã)ã
ä¾
>round ã®ä½¿ç¨
Math.round(-Infinity); // -Infinity
Math.round(-20.51); // -21
Math.round(-20.5); // -20
Math.round(-0.1); // -0
Math.round(0); // 0
Math.round(20.49); // 20
Math.round(20.5); // 21
Math.round(42); // 42
Math.round(Infinity); // Infinity
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-math.round> |