Decimals without rounding in javascript

1

How would you do to achieve the following result.

I want to decrease a number of decimal places after the comma without the number round? ex: 5.8608 - > 5,860

I have already tested various functions and forms but all have gone up to 5,861.

Thanks in advance for anyone who can contribute.

    
asked by anonymous 03.11.2017 / 19:17

1 answer

1

I did something similar here in SOpt but I can not find ... :(

Here's another suggestion. Multiply the numbers you want by the order of magnitude equal to the number of decimal places. Then you strip the decimal part and divide again in the same order of magnitude. Something like this:

function ajuste(nr, casas) {
  const og = Math.pow(10, casas)
  return Math.floor(nr * og) / og;
}

console.log(ajuste(3.456, 2)); // 3.45
console.log(ajuste(4.123, 2)); // 4.12
    
03.11.2017 / 19:23