javascript函数

转载至:http://hi.baidu.com/gradyszl/blog/item/7761bee8e188d437b90e2d62.html
//javascript格式化数字, 如 8888.85745 格式化成 8,888.86<html>
<head>
<title> New Document </title>
</head>

<body>
<script language="JavaScript">
<!--
//lael 2005-11-09
function formatnumber(fnumber,fdivide,fpoint,fround){

    var fnum = fnumber + '';
    var revalue="";

    if(fnum==null){
        for(var i=0;i<fpoint;i++)revalue+="0";
        return "0."+revalue;
    }
    fnum = fnum.replace(/^\s*|\s*$/g,'');
    if(fnum==""){
        for(var i=0;i<fpoint;i++)revalue+="0";
        return "0."+revalue;
    }

    fnum=fnum.replace(/,/g,"");

    if(fround){
        var temp = "0.";
        for(var i=0;i<fpoint;i++)temp+="0";
        temp += "5";

        fnum = Number(fnum) + Number(temp);
        fnum += '';
    }

    var arrayf=fnum.split(".");

    if(fdivide){
        if(arrayf[0].length>3){
            while(arrayf[0].length>3){
                revalue=","+arrayf[0].substring(arrayf[0].length-3,arrayf[0].length)+revalue;
                arrayf[0]=arrayf[0].substring(0,arrayf[0].length-3);
            }
        }
    }
    revalue=arrayf[0]+revalue;

    if(arrayf.length==2&&fpoint!=0){
        arrayf[1]=arrayf[1].substring(0,(arrayf[1].length<=fpoint)?arrayf[1].length:fpoint);

        if(arrayf[1].length<fpoint)
            for(var i=0;i<fpoint-arrayf[1].length;i++)arrayf[1]+="0";
        revalue+="."+arrayf[1];
    }else if(arrayf.length==1&&fpoint!=0){
        revalue+=".";
        for(var i=0;i<fpoint;i++)revalue+="0";
    }

    return revalue;
}
//-->
</script>

<input name="text1" type="text" value="" onblur="this.value=formatnumber(this.value, true, 2, true)" />
</body>
</html>
//日期比较
function datecompare(date1, date2){
var d1 = new Date(date1.replace(/\-/g, "\/"));
var d2 = new Date(date2.replace(/\-/g, "\/"));

var flag = true;

if(isNaN(d1) || isNaN(d2))flag = false;//不是日期

if(flag &&
d1.getFullYear() > d2.getFullYear()){
flag = false;
}
if(flag &&
d1.getFullYear() == d2.getFullYear() &&
d1.getMonth() > d2.getMonth()){
flag = false;
}
if(flag &&
d1.getFullYear() == d2.getFullYear() &&
d1.getMonth() == d2.getMonth() &&
d1.getDate() > d2.getDate()){
flag = false;
}
return flag;
}

你可能感兴趣的:(JavaScript,html,Blog)