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

156
Visualizações
parse decimale toFixed(2) on decimals which are not 0

The aim is to have 2 decimals without counting 0

1.2234 => 1.22

0.2345 => 0.23

0.02345 => 0.023 (instead of 0.02)

0.0000002345 => 0.00000023 (instead of 0)

0.0000002346545645645646465465 => 0.00000023 (instead of 0)

0.002035 => 0.002 instead of 0)

I want to parse toFixed(2) but on the decimals which are not 0. How to do this properly ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think from example what you meant remove last 2 digits instead of making it fixed into 2 decimals.

To do so, heres my approach.

 function removeLastDigit(number, amount = 2)
{
  const numberString = number.toString();
  if(numberString.indexOf('.') === -1) return number;
  let numberLength = numberString.length;
  for(; numberLength >= 0 && amount; numberLength--){
    if (numberString[numberLength] !== '0') amount--;
  }
  return +numberString.slice(0, numberLength);
}
let num = 0.003040;
console.log(removeLastDigit(num)) //0.03

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