JS Format数字的函数

来源:互联网 发布:淘宝做代销要钱吗 编辑:程序博客网 时间:2024/05/22 14:50

<SCRIPT LANGUAGE="JavaScript">
String.prototype.Formate = function(strValue,formatRule)
{
var spString;
var spFormate;
var        spFormateValue;
var        formatString="";
var        condition="";
var        flg="true";
var        count=0;

-----------------2007/06/22(修正)--------------------

if(strValue.length==0 || formatRule.split("#").length==0)

  {
return strValue;
     }

-----------------2007/06/22(修正)--------------------

spString=strValue.split("");
spFormate=formatRule.split(",");

var      arr      =      new      Array(spFormate.length);   

for(j=0;j<spFormate.length;j++)
{
       spFormateValue=spFormate[j].split("#");

       if(j==spFormate.length-1)
        {
arr[j]=spFormateValue.length-1;
        }else{
            for(k=j;k<spFormate.length;k++)
             {
                spFormateValue=spFormate[k].split("#");
                if(k==j)
                   {
                     arr[j]=spFormateValue.length-1;
                   }else
                        {
                          arr[j]=arr[j]+spFormateValue.length-1;
                        }
             }
}

if(j==spFormate.length-1)
{
       condition=condition+"i=="+arr[j]
           }else
           {
              condition=condition+"i=="+arr[j]+" "+"||"+" "
           }
}


for (i = strValue.length-1; i >=0 ; i--)
{

count=count+1;


     for(j=0;j<spFormate.length;j++)
      {

          if((count==arr[j]) && count !=strValue.length)
{

             formatString=","+spString[i]+formatString;
             flg="false";
          
           }
      }

        if(flg=="false")
         {
            flg="true";
           }else{
            formatString=spString[i]+formatString;
           }
         }
return formatString;
}

</script>
调用实例:

String.Formate("1234567","###,##");

原创粉丝点击