Last Updated: February 25, 2016
·
384
· scriptstar

Javascript - thousand seperator

Link

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;
}