陋室铭
永远也不要停下学习的脚步(大道至简至易)

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
        <script type="text/javascript" language="javascript">  
function FormatNumber(srcStr,nAfterDot)        //nAfterDot小数位数
{
    for(var i = 0 ; i < srcStr.length-1 ; i++)
    {
        srcStr=srcStr.replace(",","");
    }

    var srcStr,nAfterDot;
    var resultStr,nTen;
    srcStr = "" + srcStr + "" ;
    strLen = srcStr.length;
    dotPos = srcStr.indexOf(".",0);
   
    if (dotPos == -1)
    {
        resultStr = srcStr+".";
        for (i=0;i<nAfterDot;i++)
        {
            resultStr = resultStr+"0";
        }
    }
    else
    {
        if ((strLen - dotPos - 1) >= nAfterDot)
        {
            nAfter = dotPos + nAfterDot + 1;
            nTen =1;
            for(j = 0; j < nAfterDot; j++)
            {
              nTen = nTen*10;
            }
            resultStr = Math.round(parseFloat(srcStr)*nTen)/nTen;
        }
        else
        {
            resultStr = srcStr;
           
            for (i=0;i<(nAfterDot - strLen + dotPos + 1);i++)
            {
              resultStr = resultStr + "0";
            }
        }
    }
    resultStr= "" + resultStr;
    if(resultStr.indexOf(".")=="-1")
    {
        resultStr1=resultStr;
    }
    else
    {
        resultStr1 = resultStr.substring(0 , resultStr.indexOf("."));
    }
    var words = resultStr1.split(""); 
    resultStr1="";
    for( i = 0; i<words.length ;i++)
    {
        if( i!=0 && (i+1)%3 == 0)
        {
            resultStr1= "," + words[words.length-i-1] + resultStr1;
        }
        else
        {
            resultStr1= words[words.length-i-1] + resultStr1;
        }
    }
    if(words.length%3 == 0)
    {
        resultStr1=resultStr1.substring(1);
    }
    if(resultStr.indexOf(".")=="-1")
    {
        resultStr=resultStr1 + ".00";
    }
    else
    {
        resultStr=resultStr1 + resultStr.substring(resultStr.indexOf(".") , resultStr.length);
    }
    return resultStr;
     
}

       
       
       
function isDigit(s)
{
    var patrn=/^-?[0-9\,]*\.?\d*$/;
    if (!patrn.exec(s))
    {
        return false;
    }
    else
    {
        return true
    }
}

var Pre="";
function a(str)
{
    Pre=str.value;
}

function b(str)
{
    var a=str.value;
    if(a!="")
    {
        var b=isDigit(str.value);
        if(!b)
        {
            str.value=Pre;
            Label1.style.display="block";
        }
        else
        {
            Label1.style.display="none";
        }
    }
}

 

function d(str)
{

    str.value= FormatNumber(str.value,2);
    if(str.value==""||str.value==".00")
    {
    str.value="0";
    }
}

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <input id="Text1" type="text" onkeypress="a(this)" onpropertychange="b(this)" onblur="d(this)"/>
        <asp:Label ID="Label1" runat="server" Text="格式不正确" BackColor=red style="display:none"></asp:Label>
    </div>
    </form>
</body>
</html>

posted on 2008-08-04 19:54  宏宇  阅读(639)  评论(0编辑  收藏  举报