Math.trunc()
함수는 주어진 값의 소수부분을 제거하고 숫자의 정수부분을 반환합니다.
Syntax
Math.trunc(x)
Parameters
x
- 숫자형
Return value
주어진 숫자의 정수부분
Description
Math의 유사함수 3개 : Math.floor()
, Math.ceil()
and Math.round()
와는 다르게, Math.trunc()
함수는 주어진 값이 양수이건 음수이건 상관없이 소수점 이하 우측부분을 제거하는 매우 단순한 동작을 합니다.
함수인자는 암묵적으로 number형으로 변환되어 메소드에 전달됩니다.
trunc()
함수는 Math의 정적 메소드이기 때문에 사용자가 생성한 Math
객체의 메소드로 호출하는 것이 아닌 항상 Math.trunc()
형태로 호출해야 합니다. (Math
는 생성자가 아닙니다).
Examples
Using Math.trunc()
Math.trunc(13.37); // 13
Math.trunc(42.84); // 42
Math.trunc(0.123); // 0
Math.trunc(-0.123); // -0
Math.trunc('-1.123'); // -1
Math.trunc(NaN); // NaN
Math.trunc('foo'); // NaN
Math.trunc(); // NaN
Polyfill
Math.trunc = Math.trunc || function(x) {
if (isNaN(x)) {
return NaN;
}
if (x > 0) {
return Math.floor(x);
}
return Math.ceil(x);
};
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.trunc' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'Math.trunc' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.