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

137
Views
How to do thousand comma separator for javascript calculator?

I'm trying to make a thousand comma separator for JavaScript calculator.

But something works weird for me.

Here is the code that should work:

var commas = result.textContent.toLocaleString("en-US");

var commas = result.textContent.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

result.innerHTML=commas;

It works only for 1st separator, but when it goes further it looks weird. What am I doing wrong?

Here are the screenshots: image1 image2

Here is the link to calculator:

https://codesandbox.io/s/thirsty-rgb-2rp6dv?file=/js/script.js

UPD: If there's more than 4 numbers it shows NaN: NaN error

Thank you in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use Intl.NumberFormat

number = 100003.56
console.log(new Intl.NumberFormat('en-US').format(number));

Refer docs here for more

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!