DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          javascript 格式化數字

          <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 閱讀(5111) 評論(2)  編輯  收藏 所屬分類: 片段腳本語言javascript

          評論

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

          thank you very much  回復  更多評論   

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

          我找好久了>"<

          感恩啊!!大大!!  回復  更多評論   

          主站蜘蛛池模板: 绵竹市| 三亚市| 岱山县| 临夏市| 乌兰察布市| 隆回县| 漯河市| 新邵县| 从江县| 林芝县| 兴国县| 涞源县| 五指山市| 承德县| 成都市| 密山市| 济南市| 云南省| 张家川| 饶平县| 榆林市| 卢龙县| 霸州市| 西乌珠穆沁旗| 和静县| 久治县| 黎平县| 扶绥县| 祥云县| 揭西县| 周宁县| 武城县| 仪征市| 手机| 涪陵区| 乌拉特后旗| 乐东| 方城县| 北辰区| 宜阳县| 荔波县|