This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Math.trunc()

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.trunc() ์ •์  ๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž์˜ ์†Œ์ˆ˜ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•œ ์ˆซ์ž์˜ ์ •์ˆ˜ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์‹œ๋„ํ•ด ๋ณด๊ธฐ

console.log(Math.trunc(13.37));
// Expected output: 13

console.log(Math.trunc(42.84));
// Expected output: 42

console.log(Math.trunc(0.123));
// Expected output: 0

console.log(Math.trunc(-0.123));
// Expected output: -0

๊ตฌ๋ฌธ

js
Math.trunc(x)

๋งค๊ฐœ๋ณ€์ˆ˜

x

์ˆซ์ž

๋ฐ˜ํ™˜ ๊ฐ’

x์˜ ์ •์ˆ˜ ๋ถ€๋ถ„

์„ค๋ช…

๋‹ค๋ฅธ ์„ธ ๊ฐ€์ง€ Math ๋ฉ”์„œ๋“œ์ธ Math.floor(), Math.ceil() ๋ฐ Math.round()์™€ ๋‹ฌ๋ฆฌ, Math.trunc()์˜ ์ž‘๋™ ๋ฐฉ์‹์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๊ฐ€ ์–‘์ˆ˜์ด๋“  ์Œ์ˆ˜์ด๋“  ์ƒ๊ด€์—†์ด ์†Œ์ˆ˜์ ๊ณผ ๊ทธ ์˜ค๋ฅธ์ชฝ์˜ ์ˆซ์ž๋“ค์„ ์ ˆ๋‹จํ•ฉ๋‹ˆ๋‹ค(์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค).

trunc()๋Š” Math์˜ ์ •์  ๋ฉ”์„œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ์„ฑํ•œ Math ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•ญ์ƒ Math.trunc()๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค (Math๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค).

์˜ˆ์ œ

Math.trunc() ์‚ฌ์šฉํ•˜๊ธฐ

js
Math.trunc(-Infinity); // -Infinity
Math.trunc("-1.123"); // -1
Math.trunc(-0.123); // -0
Math.trunc(-0); // -0
Math.trunc(0); // 0
Math.trunc(0.123); // 0
Math.trunc(13.37); // 13
Math.trunc(42.84); // 42
Math.trunc(Infinity); // Infinity

๋น„ํŠธ ์—ฐ์‚ฐ no-ops์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž ์ž˜๋ผ๋‚ด๊ธฐ

๊ฒฝ๊ณ  : ๋ฌด์‹œํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ๊ณ„ ์กฐ๊ฑด ๋•Œ๋ฌธ์— Math.trunc()์˜ ํด๋ฆฌํ•„์ด ์•„๋‹™๋‹ˆ๋‹ค.

๋น„ํŠธ ์—ฐ์‚ฐ์€ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ 32๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์ด์šฉํ•ด ์—ญ์‚ฌ์ ์œผ๋กœ ๋ถ€๋™์†Œ์ˆ˜์  ์ˆซ์ž๋ฅผ ์ ˆ๋‹จํ•˜๋Š” ๋ฐ ํ™œ์šฉํ•ด ์™”์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๊ธฐ์ˆ ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

js
const original = 3.14;
const truncated1 = ~~original; // ์ด์ค‘ ๋ถ€์ •
const truncated2 = original & -1; // -1๊ณผ์˜ AND ๋น„ํŠธ ์—ฐ์‚ฐ
const truncated3 = original | 0; // 0๊ณผ์˜ OR ๋น„ํŠธ ์—ฐ์‚ฐ
const truncated4 = original ^ 0; // 0๊ณผ์˜ XOR ๋น„ํŠธ ์—ฐ์‚ฐ
const truncated5 = original >> 0; // 0๋งŒํผ ๋น„ํŠธ ์‹œํ”„ํŠธ

์ฃผ์˜ํ•  ์ ์€ ์ด๋Š” ๋ณธ์งˆ์ ์œผ๋กœ toInt32์™€ ๊ฐ™์ง€๋งŒ Math.trunc์™€ ๊ฐ™์ง€ ์•Š๋””๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ฐ’์ด -231 - 1 < value < 231 (-2147483649 < value < 2147483648) ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ, ๋ณ€ํ™˜ ์‹œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

js
const a = ~~2147483648; // -2147483648
const b = ~~-2147483649; // 2147483647
const c = ~~4294967296; // 0

~~๋ฅผ Math.trunc() ๋Œ€์‹  ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ž…๋ ฅ ๋ฒ”์œ„๊ฐ€ 32๋น„ํŠธ ์ •์ˆ˜์˜ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋‹ค๊ณ  ํ™•์‹ ํ•  ๋•Œ๋งŒ ์‚ฌ์šฉํ•˜์„ธ์š”.

๋ช…์„ธ์„œ

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-math.trunc

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ