Home > Mobile >  I wan't to format a string of numbers as a currency in javascript but i don't know how to
I wan't to format a string of numbers as a currency in javascript but i don't know how to

Time:12-24

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?

CodePudding user response:

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'))
  • Related