Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading