aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/math/trunc/index.html
blob: 43aa34b2d7b47ba8341cc782791998d1bf18ed12 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
title: Math.trunc()
slug: Web/JavaScript/Reference/Global_Objects/Math/trunc
tags:
  - ECMAScript 2015
  - JavaScript
  - Math
  - Method
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/trunc
---
<div>{{JSRef}}</div>

<p>Die <strong><code>Math.trunc()</code></strong> Funktion gibt den ganzzahligen Teil einer Zahl zurück, indem alle Nachkommastellen entfernt werden.</p>

<div>{{EmbedInteractiveExample("pages/js/math-trunc.html")}}</div>



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

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

<h3 id="Parameter">Parameter</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>Eine Zahl.</dd>
</dl>

<h3 id="Rückgabewert">Rückgabewert</h3>

<p>Den ganzzahligen Teil der übergebenen Zahl.</p>

<h2 id="Beschreibung">Beschreibung</h2>

<p>Im Gegensatz zu den drei <code>Math</code> Funktionen {{jsxref("Math.floor()")}}, {{jsxref("Math.ceil()")}} und {{jsxref("Math.round()")}} arbeitet <code>Math.trunc()</code> sehr einfach. Sie entfernt den Punkt und die Ziffern rechts davon, ohne zu beachten, ob es sich um eine positive oder negative Nummer handelt.</p>

<p>Beim übergeben eines Parameters wird dieser implizit in einen Nummern-Typ konvertiert.</p>

<p>Weil <code>trunc()</code> eine statische Funktion von <code>Math</code> ist, wird es immer als <code>Math.trunc()</code> eingesetzt<code>,</code> jedoch nicht als Methode eines erzeugten <code>Math</code> Objektes (<code>Math</code> ist kein Konstruktor).</p>

<h2 id="Beispiele">Beispiele</h2>

<h3 id="Einsatz_von_Math.trunc">Einsatz von <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">if(!Math.trunc) {
  Math.trunc = function(v) {
    v = +v;
    if(!isFinite(v)) return v;

    return (v - v % 1)   ||   (v &lt; 0 ? -0 : v === 0 ? v : 0);

    // returns:
    //  0        -&gt;  0
    // -0        -&gt; -0
    //  0.2      -&gt;  0
    // -0.2      -&gt; -0
    //  0.7      -&gt;  0
    // -0.7      -&gt; -0
    //  Infinity -&gt;  Infinity
    // -Infinity -&gt; -Infinity
    //  NaN      -&gt;  NaN
    //  null     -&gt;  0
  };
}</pre>

<p>oder:</p>

<pre class="brush: js line-numbers  language-js">if(!Math.trunc) {
  Math.trunc = function(v) {
    return v &lt; 0 ? Math.ceil(v) : Math.floor(v);
  }
}
</pre>

<h2 id="Spezifikationen">Spezifikationen</h2>

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

<h2 id="Browserkompatibilität">Browserkompatibilität</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="Siehe_auch">Siehe auch</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>