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

254
Visualizações
Display number with 2 decimals like excel cell in Javascript

I'm working on a web tool which makes some financial calculations and uses multiple numbers in inputs. Is there a way to display that number with only 2 decimals but behind when the app makes the math to use the entire number with all decimals? Something like excel cell which shows 2 decimals but behind the number has 10 decimals.

The problem is that if I round the numbers, the app gives incorrect values. It's just for display purpose to make it look neat and tidy.

Thx.

For ex:

Input real value: 8952340.66543023 Input display value: 8952340.67

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

0

You could write a general function which can round to any number of digits :

const roundTo =
    num =>
    digits =>
    Math.round(num * (10 ** digits)) / 10 ** digits

const num = 1234.8304

const roundToTwo = roundTo(num)(2) 

console.log(roundToTwo) // logs 1234.83

Where num is your number and digits is the n of digits after the comma.

about 4 years ago · Juan Pablo Isaza Relatório

0

const f1 = new Intl.NumberFormat('en-US', {
  minimumFractionDigits: 2,      
  maximumFractionDigits: 2,
});

console.log("first -> ", f1.format(8952340.66543023));

const f2 = (n, d) => n.toLocaleString('en-US', {
   minimumFractionDigits: 2,      
   maximumFractionDigits: 2,
});

console.log("Second -> ", f2(8952340.66543023));

Try this code, and tell me.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use toFixed(2) to reduce precision to two decimals.

but beware the return value is a string not a number

let float = 1.23456789;
let fixed = float.toFixed(2);
console.log(fixed );

console.log(typeof(fixed))

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