Last active
August 3, 2020 08:09
-
-
Save mubasshir/87cb715a8c3c502b82086b1b536a28b1 to your computer and use it in GitHub Desktop.
Indian Rupee (INR) - comma separator
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
function formatNumber(numberToFormat) { | |
// converting to string | |
numberToFormat = numberToFormat + '' || ''; | |
// to make it works for integer and floating as well | |
var numberAndDecimal = numberToFormat.split('.'); | |
// decimals points only | |
var decimals = numberAndDecimal[1] || null; | |
// add commas | |
numberAndDecimal = numberAndDecimal[0].replace(/(\d)(?=(\d\d)+\d$)/g, "$1,"); | |
// join | |
numberToFormat = decimals ? numberAndDecimal + '.' + decimals : numberAndDecimal; | |
// return formatted | |
return numberToFormat; | |
} | |
alert(formatNumber(prompt("Enter Number", 1234567.89))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment