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?
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?
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'))
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);
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;
}