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

176
Views
Javascript number format to decimal

I have a question about decimal numbers. I need to convert my numbers to decimal but I couldn't get what I exactly want.

The numbers that I used on my project.

What I want is:

  • I want to convert 130000 to 130,000 and 20911.56 to 20,911,56 (etc.)

  • First of all I searched in here and found some solutions to change my numbers :

          function number_format(string,decimals=2,decimal=',',thousands='.',pre='R$ ',pos=' Reais'){
              var numbers = string.toString().match(/\d+/g).join([]);
              numbers = numbers.padStart(decimals+1, "0");
              var splitNumbers = numbers.split("").reverse();
              var mask = '';
              splitNumbers.forEach(function(d,i){
                  if (i == decimals) { mask = decimal + mask; }
                  if (i>(decimals+1) && ((i-2)%(decimals+1))==0) { mask = thousands + mask; }
                  mask = d + mask;
              });
              return pre + mask + pos;
          }
          var element = document.getElementById("format");
    
           var money= number_format("130000",2,',','.');
          element.innerHTML = money;
    

This code above gave me 20.911,56 but it didn't give me 130,000. Instead it is 1,300,00.What should I do? Can't I have them on the same time?

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

0

Just use Intl.NumberFormat as follows:

const formatter = new Intl.NumberFormat('en');

console.log(formatter.format(130000)); // 130,000
console.log(formatter.format(20911.56)); // 20,911.56

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!