Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
Javascript - Max 4 Digits after Comma

I`m looking for a way to delete the last Digits from an Input with comma if the Value is more then 4.

Example:

Value : 13,314556 should be -> 13,3145.

Is there a way to just remove everything then the last 4 digits of this number with something like this?

     let value = 13,314556
        let result= value.split(',')[1].trim();
        if(result.length > 4){
          ...
        }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

toFixed()

You can call toFixed() to format the number to a number of decimal places.

See MDN Documentation for toFixed()

Modified from the example given in the above documentation:

function myFormat(x) {
  return Number.parseFloat(x).toFixed(4);
}

// expected output: "13.3146" because of rounding
console.log(myFormat(13.314556));

// expected output: "0.0040"
console.log(myFormat(0.004));

// expected output: "123000.0000"
console.log(myFormat('1.23e+5'));

If you don't want rounding, simply be more precise and clip the end of the resulting string.

    function myFormat(x) {
      return Number.parseFloat(x).toFixed(6).replace(/\d\d$/, '');
    }

    // expected output: "13.3145", will not do rounding
    console.log(myFormat(13.314556));

If you are working with currency or do similar important calculations, consider working with a library like decimal.js.

about 4 years ago · Juan Pablo Isaza Relatório

0

alternative is to multiply by 10000, floor it, then divide by 10000.

let value = 13.314556;
value=Math.floor(value*10000)/10000;
console.log(value)

or if you want to control the number of digits programmatically you could use something like this:

let value = 13.314556;
value=Math.floor(value*Math.pow(10, 4))/Math.pow(10, 4);
console.log(value)

about 4 years ago · Juan Pablo Isaza Relatório

0

I would do it with the slice and join methods

Slice for slicing the string with the length you want

Join to re-create the number as you wish


Edit

I added check if comma is in the number and also a parameter with the desired length

function splitNumber(num, nbAfterComa) {

  const number = num.toString()
  if(!number.includes('.')){
    return number
  }
  const splitedValue = number.split('.')
  splitedValue[1] = splitedValue[1].trim().slice(0, nbAfterComa);
  return nbAfterComa > 0 ? splitedValue.join(',') : splitedValue[0]
}

console.log(splitNumber(13.314556, 4))
console.log(splitNumber(13.3, 4))
console.log(splitNumber(13.314556, 1))
console.log(splitNumber(13.314556, 0))
console.log(splitNumber(13, 8))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda