function formatNum(str) {
var newStr = "";
var count = 0;
// 當數字是整數
if (str.indexOf(".") == -1) {
for (var i = str.length - 1; i >= 0; i--) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + "," + newStr;
} else {
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + ".00"; //自動補小數點後兩位
return str;
}
// 當數字帶有小數
else {
for (var i = str.indexOf(".") - 1; i >= 0; i--) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + "," + newStr;
} else {
newStr = str.charAt(i) + newStr; //逐個字符相接起來
}
count++;
}
str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);
return str;
}
}
js 數字三位加逗點
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.