Created
September 25, 2018 10:33
-
-
Save XDestination/bd75bb913ab8f4bf169facc502d2a95d to your computer and use it in GitHub Desktop.
format a number with thousand separators
This file contains 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
Number.prototype.withThousandSep = function() { | |
return (this + '') | |
.split('') | |
.reverse() | |
.reduce(function(carry, d, index) { | |
if (index > 0 && index % 3 === 0) { | |
carry.push('.'); | |
} | |
carry.push(d); | |
return carry; | |
}, []) | |
.reverse() | |
.join(''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment