/* 千位分隔符 */
function setNoSeparate (no) {
let num = no || 0;
let numArr = num.toString().split(‘.’);
let integers = numArr[0].replace(/\B(?=(\d{3})+ ) | (^-?[1-9][0-9]* )/).test(value + ”);
}
/*
* @desc 轉換成數字,可以輸入負數或小數
* @param {v} [String] 要轉換的字符串
* @param {places} [Number] 默認小數後兩位
* @return {b} [Boolean] true可以包括負數,false不包括負數
* 觸發事件只能是keyup事件
* */
function formatNo(v, places = 2, b=false) {
const p = Number(places) + 1
const _ = b ? ‘^\-’ : ”;
const reg = new RegExp(‘[^\d’ + _ + ‘^\.]’, ‘g’);
v = String(v).replace(reg, ”);
if (!v) return ”;
let [ds, dl, ms, ml] = [v.indexOf(‘.’), v.lastIndexOf(‘.’), v.indexOf(‘-‘), v.lastIndexOf(‘-‘)];
while (ds !== dl) {
v = v.slice(0, dl);
ds = v.indexOf(‘.’);
dl = v.lastIndexOf(‘.’);
}
while (ms !== ml) {
v = v.slice(0, ml);
ms = v.indexOf(‘-‘);
ml = v.lastIndexOf(‘-‘);
}
v = v.includes(‘-‘) && ms ? v.slice(0, ms) : v;
v = v.startsWith(‘.’) ? 0 + v : (v[0] === ‘-’ && v[1] === ‘.’) ? ‘-0’ + v.slice(ds) : v;
return v.slice(0, v.includes(‘.’) ? ds + p : v.length);
}
js日常開發的方法2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.