aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/global_objects/math/trunc/index.html
blob: 0f63fd1e58ddb8cc844de0e6f81a3c5d217e1918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: Math.trunc()
slug: Web/JavaScript/Reference/Global_Objects/Math/trunc
translation_of: Web/JavaScript/Reference/Global_Objects/Math/trunc
---
<div>{{JSRef}}</div>

<p><strong><code>Math.trunc()</code></strong> 함수는 주어진 값의 소수부분을 제거하고 숫자의 정수부분을 반환합니다.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">Math.trunc(<var>x</var>)
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>숫자형</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>주어진 숫자의 정수부분</p>

<h2 id="Description">Description</h2>

<p>Math의 유사함수 3개 : {{jsxref("Math.floor()")}}, {{jsxref("Math.ceil()")}} and {{jsxref("Math.round()")}} 와는 다르게, <code>Math.trunc()</code> 함수는 주어진 값이 양수이건 음수이건 상관없이 소수점 이하 우측부분을 제거하는 매우 단순한 동작을 합니다.</p>

<p>함수인자는 암묵적으로 number형으로 변환되어 메소드에 전달됩니다.</p>

<p><code>trunc()</code> 함수는 Math의 정적 메소드이기 때문에 사용자가 생성한 <code>Math</code> 객체의 메소드로 호출하는 것이 아닌 항상 <code>Math.trunc()</code> 형태로 호출해야 합니다. (<code>Math</code> 는 생성자가 아닙니다).</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_Math.trunc()">Using <code>Math.trunc()</code></h3>

<pre class="brush: js">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
</pre>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">Math.trunc = Math.trunc || function(x) {
  if (isNaN(x)) {
    return NaN;
  }
  if (x &gt; 0) {
    return Math.floor(x);
  }
  return Math.ceil(x);
};</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-math.trunc', 'Math.trunc')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.trunc', 'Math.trunc')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("javascript.builtins.Math.trunc")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Math.abs()")}}</li>
 <li>{{jsxref("Math.ceil()")}}</li>
 <li>{{jsxref("Math.floor()")}}</li>
 <li>{{jsxref("Math.round()")}}</li>
 <li>{{jsxref("Math.sign()")}}</li>
</ul>