Skip to content

Instantly share code, notes, and snippets.

@mubasshir
Last active August 3, 2020 08:09
Show Gist options
  • Save mubasshir/87cb715a8c3c502b82086b1b536a28b1 to your computer and use it in GitHub Desktop.
Save mubasshir/87cb715a8c3c502b82086b1b536a28b1 to your computer and use it in GitHub Desktop.
Indian Rupee (INR) - comma separator
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