function formatnumber(value, num){
let _value = value.toString();
let _dot = _value.indexOf(".");
let _valueLen = _value.length;
if (num == 0) {
if (_dot != -1) {
_value = _value.substring(0, _dot);
}
} else {//若是沒有小數點
if (_dot == -1) {
_value = _value + ".";
for (let i = 1; i <= num; i++) {
_value = _value + "0";
}
} else {//有小數點,超出位數自動截取,不然補0
_value = _value.substring(0, _dot + num + 1);
for (let i = _valueLen; i <= _dot + num; i++) {
_value = _value + "0";
}
}
}
return _value;
}
formatnumber(3.1,2)複製代碼