what works in java script allows me to truncate a value?

-2

I need a function in JS that allows me to truncate 4.5 to 4 for example something like .truncate(x)

    
asked by vnzlalibre 03.10.2017 в 14:26
source

3 answers

5

Use Math.trunc();

let miNumero = 4.5;
let truncado = Math.trunc(miNumero);
console.log("el numero: " +miNumero , "se trunco : " + truncado );
  
    
answered by 03.10.2017 в 14:27
0

You can use Math.floor(n) to tune n to 4, in this case

Math.floor(4.5); //Devuelve 4
    
answered by 03.10.2017 в 16:46
0

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)
    
answered by 04.02.2018 в 14:27