Created
June 17, 2015 20:00
-
-
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"
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
//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