Javascript - thousand seperator
In one of my project, I was calculating distance at the GUI level and I need a javascript function to seperate the thousands and there aren't many good example when I scout thruough internet and I decided to write one for myself using regular expressions.
function thousandsSeparator(val,sep) {
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'),
sValue=val+'';
if (sep === undefined) {sep=',';}
while(sRegExp.test(sValue)) {
sValue = sValue.replace(sRegExp, '$1'+sep+'$2');
}
return sValue;
}
Written by Narendra
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript - thousand seperator
Authors
Related Tags
#javascript - thousand seperator
#thousand seperator
#javascript - thousand seperator regular expression
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#