blob: 64c918039a42fbf4ff10457214e34db54b443de7 (
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
|
---
title: Math.trunc()
slug: Web/JavaScript/Reference/Global_Objects/Math/trunc
tags:
- ECMAScript 2015
- JavaScript
- Math
- Méthode
- Reference
- polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Math/trunc
original_slug: Web/JavaScript/Reference/Objets_globaux/Math/trunc
---
{{JSRef}}
La fonction **`Math.trunc()`** retourne la troncature entière d'un nombre en retirant sa partie décimale.
<math><semantics><mrow><mstyle mathvariant="monospace"><mrow><mo lspace="0em" rspace="thinmathspace">Math.trunc</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><mrow><mo>{</mo><mtable columnalign="left left"><mtr><mtd><mrow><mo>⌊</mo><mi>x</mi><mo>⌋</mo></mrow></mtd><mtd><mtext>si</mtext></mtd><mtd><mi>x</mi><mo>≥</mo><mn>0</mn></mtd></mtr><mtr><mtd><mrow><mo>⌈</mo><mi>x</mi><mo>⌉</mo></mrow></mtd><mtd><mtext>si</mtext></mtd><mtd><mi>x</mi><mo><</mo><mn>0</mn></mtd></mtr></mtable></mrow></mrow><annotation encoding="TeX">\mathtt{\operatorname{Math.trunc}(x)} = \begin{cases} \left\lfloor x \right\rfloor & \text{if} & x \geq 0 \\ \left\lceil x \right\rceil & \text{if} &x < 0 \end{cases}</annotation></semantics></math>
{{EmbedInteractiveExample("pages/js/math-trunc.html")}}
## Syntaxe
Math.trunc(x)
### Paramètres
- `x`
- : Un nombre.
### Valeur de retour
La partie entière du nombre passé en argument.
## Description
Contrairement aux autres méthodes {{jsxref("Math.floor()")}}, {{jsxref("Math.ceil()")}} et {{jsxref("Math.round()")}}, `Math.trunc()` fonctionne de façon très simple : la partie décimale du nombre est retirée et on conserve la partie entière (que le nombre soit positif ou négatif).
Ainsi, si l'argument est un nombre positif, `Math.trunc()` sera équivalent à `Math.floor()`, sinon `Math.trunc()` sera équivalent à `Math.ceil()`.
On notera que l'argument passé à la méthode est converti en nombre de façon implicite.
`trunc()` est une méthode statique de `Math`, elle doit toujours être utilisée avec la syntaxe `Math.trunc()`, elle ne doit pas être utilisée comme la méthode d'un objet qui aurait été instancié (`Math` n'est pas un constructeur).
## Exemples
### Utiliser `Math.trunc()`
```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("toto"); // NaN
Math.trunc(); // NaN
```
## Spécifications
| Spécification | État | Commentaire |
| ---------------------------------------------------------------------------- | ---------------------------- | -------------------- |
| {{SpecName('ES2015', '#sec-math.trunc', 'Math.trunc')}} | {{Spec2('ES2015')}} | Première définition. |
| {{SpecName('ESDraft', '#sec-math.trunc', 'Math.trunc')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.Math.trunc")}}
## Voir aussi
- {{jsxref("Math.abs()")}}
- {{jsxref("Math.ceil()")}}
- {{jsxref("Math.floor()")}}
- {{jsxref("Math.round()")}}
- {{jsxref("Math.sign()")}}
|