DANCE WITH JAVA

          開發(fā)出高質(zhì)量的系統(tǒng)

          常用鏈接

          統(tǒng)計(jì)

          積分與排名

          好友之家

          最新評(píng)論

          javascript 格式化數(shù)字

          <script>

          function formatNumber(number,pattern){
              
          var str            = number.toString();
              
          var strInt;
              
          var strFloat;
              
          var formatInt;
              
          var formatFloat;
              
          if(/\./g.test(pattern)){
                  formatInt        
          = pattern.split('.')[0];
                  formatFloat        
          = pattern.split('.')[1];
              }
          else{
                  formatInt        
          = pattern;
                  formatFloat        
          = null;
              }


              
          if(/\./g.test(str)){
                  
          if(formatFloat!=null){
                      
          var tempFloat    = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);
                      strInt        
          = (Math.floor(number)+Math.floor(tempFloat)).toString();                
                      strFloat    
          = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';            
                  }
          else{
                      strInt        
          = Math.round(number).toString();
                      strFloat    
          = '0';
                  }

              }
          else{
                  strInt        
          = str;
                  strFloat    
          = '0';
              }

              
          if(formatInt!=null){
                  
          var outputInt    = '';
                  
          var zero        = formatInt.match(/0*$/)[0].length;
                  
          var comma        = null;
                  
          if(/,/g.test(formatInt)){
                      comma        
          = formatInt.match(/,[^,]*/)[0].length-1;
                  }

                  
          var newReg        = new RegExp('(\\d{'+comma+'})','g');

                  
          if(strInt.length<zero){
                      outputInt        
          = new Array(zero+1).join('0')+strInt;
                      outputInt        
          = outputInt.substr(outputInt.length-zero,zero)
                  }
          else{
                      outputInt        
          = strInt;
                  }


                  
          var 
                  outputInt            
          = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1')
                  outputInt            
          = outputInt.replace(/^,/,'');

                  strInt    
          = outputInt;
              }


              
          if(formatFloat!=null){
                  
          var outputFloat    = '';
                  
          var zero        = formatFloat.match(/^0*/)[0].length;

                  
          if(strFloat.length<zero){
                      outputFloat        
          = strFloat+new Array(zero+1).join('0');
                      
          //outputFloat        = outputFloat.substring(0,formatFloat.length);
                      var outputFloat1    = outputFloat.substring(0,zero);
                      
          var outputFloat2    = outputFloat.substring(zero,formatFloat.length);
                      outputFloat        
          = outputFloat1+outputFloat2.replace(/0*$/,'');
                  }
          else{
                      outputFloat        
          = strFloat.substring(0,formatFloat.length);
                  }


                  strFloat    
          = outputFloat;
              }
          else{
                  
          if(pattern!='' || (pattern=='' && strFloat=='0')){
                      strFloat    
          = '';
                  }

              }


              
          return strInt+(strFloat==''?'':'.'+strFloat);
          }


          alert(formatNumber(
          0,''));
          alert(formatNumber(
          12432.21,'#,###'));
          alert(formatNumber(
          12432.21,'#,###.000#'));
          alert(formatNumber(
          12432,'#,###.00'));
          alert(formatNumber('
          12432.415','#,###.0#'));


          </script>

          posted on 2007-09-10 22:02 dreamstone 閱讀(5126) 評(píng)論(2)  編輯  收藏 所屬分類: 片段腳本語言javascript

          評(píng)論

          # re: javascript 格式化數(shù)字 2009-02-11 16:37 zhujianf

          thank you very much  回復(fù)  更多評(píng)論   

          # re: javascript 格式化數(shù)字 2009-03-12 10:11 弓箭手

          我找好久了>"<

          感恩啊!!大大!!  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 宁波市| 桐乡市| 称多县| 信宜市| 镇远县| 曲阜市| 锡林浩特市| 九江市| 金塔县| 兴仁县| 玉门市| 疏勒县| 九江县| 宜昌市| 宁河县| 湖北省| 淳化县| 天津市| 永济市| 双城市| 阜宁县| 蓝山县| 沅陵县| 新安县| 竹山县| 门源| 保山市| 陆河县| 沙河市| 黔西| 昭苏县| 柳江县| 佛学| 周宁县| 明星| 南陵县| 广安市| 马公市| 江源县| 上栗县| 宿松县|