I need assistance in converting a number to Indian currency format.
Currently, I have attempted the following: http://plnkr.co/edit/lVJGXyuX0BMvB9QUL5zS?p=preview
function formatMoney(credits) {
console.log(credits+'credits');
var lastThree = credits.substring(credits.length-3);
// var lastThree = credits.slice(-2);
var otherNumbers = credits.substring(0,credits.length-3);
console.log(otherNumbers+'otherNumbers');
if(otherNumbers !== '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return res;
}
function formatNumber(num) {
var n1, n2;
num = (Math.round(num * 100) / 100) + '' || '';
n1 = num.split('.');
n2 = n1[1] || null;
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
num = n2 ? n1 + '.' + n2 : n1;
return num;
}
I am specifically looking to format the number as Indian currency.
For example:
1,000
10,000
1,00,000
10,00,000
The result obtained with the function formatMoney() is: 1,0,0,000
The result obtained with the function formatNumber() is: 1,000 but on adding a 0, it becomes NaN0
I would appreciate any guidance on where I might be going wrong in my approach.