function toCurrency(nStr,places) {
    var len = 0;
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    if(x.length < 2){
        len = 0;
        x[1] = '';   
    }
    else{
        len = x[1].length;
    }
    if (places > 0){ 
        x[1] += str_rep('0',places - len);
    }  
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return '$' + x1 + x2;
}

function str_rep(str,n) {   
    var s = "";       
    while (--n >= 0) {
        s += str;
    }
    return s
}
