本帖最后由 icarrien 于 2011-05-04 15:05:23 编辑

解决方案 »

  1.   

    function number(v) {
        /*
         * 如果是小数,想四舍五入就把下一句取消注释
         */
    //    v = (Math.round((v-0)*100))/100;
        v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
        v = String(v);
        var ps = v.split('.'),
            whole = ps[0],
            sub = ps[1] ? ps[1] : '',
            r = /(\d+)(\d{3})/;
        /*
         * 格式化整数部分
         */
        while (r.test(whole)) {
            whole = whole.replace(r, '$1' + ',' + '$2');
        }
        /*
         * 格式化小数部分
        r=/(\d{3})(\d+)/;
        while(r.test(sub)){
            sub = sub.replace(r, '$1' + ',' + '$2');
        }
         */
        v = whole +'.'+ sub;
        
        
        if(v.charAt(0) == '-'){
            return '-' + v.substr(1);
        }
        return v;
    }
      

  2.   

      <script type="text/javascript">
    var a=115151342343246.213;
    function format(num){
    var temp=(""+num).split('.')[0];
    var yu=temp.length%3;
    var fin=temp.substr(0,yu)+temp.substr(yu).replace(/(\d{3})/g,",$1")+"."+(""+num).split('.')[1];
    if(yu==0){
    fin=fin.substr(1);
    }alert(fin);
    }format(a);  </script>
      

  3.   

       String.prototype.format = function(){
         return this.replace(/\d(?=(?:\d{3})+\b)/g,function(v) {
    return v + ',';
    });
    }
    var a = 76884526.88;
    alert(a.toString().format()); //return 67,884,526.88
      

  4.   

    问一下兄弟。
      if(v.charAt(0) == '-'){
    return '-' + v.substr(1);
      }
    检测字符串第一位的字符是否为"-",如果是就进行返回‘-’加1后的所有字符串。
    他本来就已经是正确的数了,为什么还要这样的操作呢。
      

  5.   

    没那么复杂,就一行正则:
    NumberString.replace(/(?!^)(?=(\d{3})+\.)/g, "$&,")TEST:
    alert("25.88"       .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
    alert("2555.88"     .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
    alert("76884526.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
    alert("768845266.88".replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
      

  6.   


    if
      var a = 76884526.8876767;
    then
      alert(a.toString().format()) ===> 76,884,526.8,876,767     ????
    endif
      

  7.   


    这位兄弟的方法正确。
    现提供一个可匹配不带小数点的方法
    "2522323.88".replace(/(?=(\d{3})+(\.\d+)?$)/g,",") 
    不过此方法也有bug,就是当小数位数超过两位的时候,小数位也会被添加逗号。
    js好象不支持后顾断言,所以没想到好的办法