Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda