一再——一个老网友朋友给我的例子代码。能对这种0.0756035338495136 数据格式化为7.5% 输出。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Picker Test</title>
<script type="text/javascript" >
function FormatNumber(srcStr,nAfterDot){
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";
}
return resultStr;
}
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;
return resultStr;
}
else{
resultStr = srcStr;
for (i=0;i<(nAfterDot - strLen + dotPos + 1);i++){
resultStr = resultStr+"0";
}
return resultStr;
}
}
}
</script>
</head>
<body>
<p>
<input type="button" name="Button" value="SHOW VALUE" onclick="javascript:alert(FormatNumber(0.0756035338495136*100,2)+'%');"/>
</p>
</body>
</html>
posted on 2007-06-02 18:34
-274°C 阅读(1722)
评论(2) 编辑 收藏 所属分类:
web前端