I need a function in JS that allows me to truncate 4.5
to 4
for example
something like .truncate(x)
I need a function in JS that allows me to truncate 4.5
to 4
for example
something like .truncate(x)
Use Math.trunc();
let miNumero = 4.5;
let truncado = Math.trunc(miNumero);
console.log("el numero: " +miNumero , "se trunco : " + truncado );
You can use Math.floor(n)
to tune n to 4, in this case
Math.floor(4.5); //Devuelve 4
The correct answer to what @vnzlalibre was asking is the following: use the function toFixed
, as in the following example:
var numero = 5.56789;
var conDecimal = numero.toFixed(2);
// Igual a 5.57
var entero = numero.toFixed();
// Igual a 5 (como un entero)