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

289
Views
I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string

so what i mean is:

1000 should become 10,00
10000 should become 100,00
100000 should become 1000,00

Can some help me out?

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

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#using_options

var number = 1000;

// request a currency format
console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));

Without methods and just formatting with commas

        function addComma(value) {
            return value.split('').map((v, i) => {
                if ( i === value.length - 2) {
                    return ',' + v
                }
                return v
            }).join('')
        }
        console.log(addComma('1000'))
        console.log(addComma('10000'))
        console.log(addComma('100000'))
about 4 years ago · Juan Pablo Isaza Report

0

https://www.delftstack.com/howto/javascript/javascript-currency-format/

you can transform your string in number and then use toLocaleString()

here is an example from the webpage

const number = 2000;
number.toLocaleString('en-IN', {style: 'currency',currency: 'INR', minimumFractionDigits: 2})
console.log(number);
about 4 years ago · Juan Pablo Isaza Report

0

Another solution, just splitting the string into whole and cents:

function addComma(n)
{
    if (n.length == 0) {
       return '0,00';
    }

    if (n.length == 1) {
       return '0,0' + n;
    }

    if (n.length == 2) {
       return '0,' + n;
    }

    let whole = n.substring(0, n.length-2);
    let cents = n.substring(n.length-2);
    return whole + ',' + cents;
}
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!