Skip to content

Instantly share code, notes, and snippets.

@teckliew
Created June 17, 2015 20:00
Show Gist options
  • Save teckliew/de02fb3b2956841c6b8d to your computer and use it in GitHub Desktop.
Save teckliew/de02fb3b2956841c6b8d to your computer and use it in GitHub Desktop.
a function that takes an integer in input and outputs a string with currency format. Ex. 123456 -> "123,456"
//my solution using REGEX
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
//good example
function toCurrency(price){
return price.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment