js 進一法、四捨五入法、捨去法 保留兩位小數
知識
10-23
function format_number(num,len) {
len = len > 0 && len <= 20 ? len : 2;
var result = parseFloat(num);
num=isNaN(result)?0:result;
var numpow=Math.pow(10,len);
result = Math.round(num * numpow) / numpow;//round四捨五入 ceil進一 floor捨去
var s_x = result.toString();
var pos_decimal = s_x.indexOf(".");
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += ".";
}
while (s_x.length <= pos_decimal + len) {s_x += "0";}
return s_x;
}
alert(format_number(" 222.a ",3));
可根據提示(round四捨五入 ceil進一 floor捨去) 修改成是任意想要的方法
Seo 技巧顯示搜索引擎 3d 圖
![](https://pic.pimg.tw/zzuyanan/1488615166-1259157397.png)
![](https://pic.pimg.tw/zzuyanan/1482887990-2595557020.jpg)
※我們一直談論「寫代碼」,但你會「讀代碼」嗎?
※錯誤 could not launch process: EOF 解決
TAG:程序員小新人學習 |