Math.trunc()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La fonction Math.trunc() retourne la troncature entière d'un nombre en retirant sa partie décimale.

Math.trunc(x)={xsix0xsix<0\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}

Exemple interactif

console.log(Math.trunc(13.37));
// Expected output: 13

console.log(Math.trunc(42.84));
// Expected output: 42

console.log(Math.trunc(0.123));
// Expected output: 0

console.log(Math.trunc(-0.123));
// Expected output: -0

Syntaxe

js
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 Math.floor(), Math.ceil() et 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

Specification
ECMAScript® 2025 Language Specification
# sec-math.trunc

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
trunc

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi