Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!