diff options
Diffstat (limited to 'files/fr/web/javascript/reference/global_objects/math/trunc/index.md')
-rw-r--r-- | files/fr/web/javascript/reference/global_objects/math/trunc/index.md | 101 |
1 files changed, 42 insertions, 59 deletions
diff --git a/files/fr/web/javascript/reference/global_objects/math/trunc/index.md b/files/fr/web/javascript/reference/global_objects/math/trunc/index.md index c78b0cb6ec..64c918039a 100644 --- a/files/fr/web/javascript/reference/global_objects/math/trunc/index.md +++ b/files/fr/web/javascript/reference/global_objects/math/trunc/index.md @@ -11,84 +11,67 @@ tags: translation_of: Web/JavaScript/Reference/Global_Objects/Math/trunc original_slug: Web/JavaScript/Reference/Objets_globaux/Math/trunc --- -<div>{{JSRef}}</div> +{{JSRef}} -<p>La fonction <code><strong>Math.trunc()</strong></code> retourne la troncature entière d'un nombre en retirant sa partie décimale.</p> +La fonction **`Math.trunc()`** retourne la troncature entière d'un nombre en retirant sa partie décimale. -<p><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></p> +<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> -<div>{{EmbedInteractiveExample("pages/js/math-trunc.html")}}</div> +{{EmbedInteractiveExample("pages/js/math-trunc.html")}} -<h2 id="Syntaxe">Syntaxe</h2> +## Syntaxe -<pre class="syntaxbox">Math.trunc(<var>x</var>)</pre> + Math.trunc(x) -<h3 id="Paramètres">Paramètres</h3> +### Paramètres -<dl> - <dt><code>x</code></dt> - <dd>Un nombre.</dd> -</dl> +- `x` + - : Un nombre. -<h3 id="Valeur_de_retour">Valeur de retour</h3> +### Valeur de retour -<p>La partie entière du nombre passé en argument.</p> +La partie entière du nombre passé en argument. -<h2 id="Description">Description</h2> +## Description -<p>Contrairement aux autres méthodes {{jsxref("Math.floor()")}}, {{jsxref("Math.ceil()")}} et {{jsxref("Math.round()")}}, <code>Math.trunc()</code> 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).<br> - <br> - Ainsi, si l'argument est un nombre positif, <code>Math.trunc()</code> sera équivalent à <code>Math.floor()</code>, sinon <code>Math.trunc()</code> sera équivalent à <code>Math.ceil()</code>.</p> +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). -<p>On notera que l'argument passé à la méthode est converti en nombre de façon implicite.</p> +Ainsi, si l'argument est un nombre positif, `Math.trunc()` sera équivalent à `Math.floor()`, sinon `Math.trunc()` sera équivalent à `Math.ceil()`. -<p><code>trunc()</code> est une méthode statique de <code>Math</code>, elle doit toujours être utilisée avec la syntaxe <code>Math.trunc()</code>, elle ne doit pas être utilisée comme la méthode d'un objet qui aurait été instancié (<code>Math</code> n'est pas un constructeur).</p> +On notera que l'argument passé à la méthode est converti en nombre de façon implicite. -<h2 id="Exemples">Exemples</h2> +`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). -<h3 id="Utiliser_Math.trunc()">Utiliser <code>Math.trunc()</code></h3> +## Exemples -<pre class="brush:js">Math.trunc(13.37); // 13 +### 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</pre> - -<h2 id="Spécifications">Spécifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Spécification</th> - <th scope="col">État</th> - <th scope="col">Commentaire</th> - </tr> - <tr> - <td>{{SpecName('ES2015', '#sec-math.trunc', 'Math.trunc')}}</td> - <td>{{Spec2('ES2015')}}</td> - <td>Première définition.</td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-math.trunc', 'Math.trunc')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td></td> - </tr> - </tbody> -</table> - -<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2> - -<p>{{Compat("javascript.builtins.Math.trunc")}}</p> - -<h2 id="Voir_aussi">Voir aussi</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> +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()")}} |