Last active
May 24, 2016 09:17
-
-
Save rajvanshipradeep15/2f2cc06b6e302a44d73f91ce32b25d02 to your computer and use it in GitHub Desktop.
Format number in input box with comma
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(Number(monthly_income).toLocaleString()); | |
console.log(Number(monthly_income).toLocaleString('en')); | |
toLocaleString is obselete | |
https://syedabdulbaqi.wordpress.com/2009/03/10/javascript-function-for-converting-string-into-indian-currency-format/ | |
//function for converting string into indian currency format | |
function intToFormat(nStr) | |
{ | |
nStr += ''; | |
x = nStr.split('.'); | |
x1 = x[0]; | |
x2 = x.length > 1 ? '.' + x[1] : ''; | |
var rgx = /(\d+)(\d{3})/; | |
var z = 0; | |
var len = String(x1).length; | |
var num = parseInt((len/2)-1); | |
while (rgx.test(x1)) | |
{ | |
if(z > 0) | |
{ | |
x1 = x1.replace(rgx, '$1' + ',' + '$2'); | |
} | |
else | |
{ | |
x1 = x1.replace(rgx, '$1' + ',' + '$2'); | |
rgx = /(\d+)(\d{2})/; | |
} | |
z++; | |
num--; | |
if(num == 0) | |
{ | |
break; | |
} | |
} | |
return x1 + x2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment