PL
JavaScript - Math.trunc() - przykład metody z dokumentacją
4
points
Math.trunc()
metoda zwraca całkowitą część liczby.
// ONLINE-RUNNER:browser;
console.log( Math.trunc( 5 )); // 5
console.log( Math.trunc( 3.14 )); // 3
console.log( Math.trunc( -3.14 )); // -3
console.log( Math.trunc( 0.123 )); // 0
console.log( Math.trunc( -0.123 )); // -0
console.log( Math.trunc( '+3.14' ) ); // 3.14
console.log( Math.trunc( '-3.14' ) ); // -3.14
console.log( Math.trunc( NaN ) ); // NaN
console.log( Math.trunc( 'foo' ) ); // NaN
console.log( Math.trunc() ); // NaN
1. Dokumentacja
Składnia | Math.trunc(number) |
Parametry | number - liczba całkowita lub zmiennoprzecinkowa (wartość pierwotna). |
Wynik |
Całkowita część wartości liczbowej (wartość pierwotna). Jeśli liczba ma wartość Jeśli liczba to Jeśli liczba to |
Opis |
|
2. Przykład wypełnienia
// ONLINE-RUNNER:browser;
if (Math.trunc == null) {
Math.trunc = function (v) {
return v < 0 ? Math.ceil(v) : Math.floor(v);
};
}
// Przykład użycia:
console.log( Math.trunc( 5 )); // 5
console.log( Math.trunc( 3.14 )); // 3
console.log( Math.trunc( -3.14 )); // -3
console.log( Math.trunc( 0.123 )); // 0
console.log( Math.trunc( -0.123 )); // -0