标题input输入框输入数字或小数后每隔三位按逗号分隔,小数只能输入两位
controlAmount(value
) {
if(value
!=''){
value
= value
.replace(/[^\d.]/g, '');
value
= value
.replace(/^\./g, '');
value
= value
.replace(/\.{2,}/g, '.');
value
= value
.replace('.', '$#$')
.replace(/\./g, '')
.replace('$#$', '.')
.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
if (value
.indexOf('.') < 0 && value
!= '') {
value
=this.format_number(value
)
}else{
let left
= this.format_number(value
.split('.')[0]),
right
= value
.split('.')[1];
value
= left
+ '.' + right
;
}
this.money
=value
}
},
format_number(n
) {
var b
= parseInt(n
).toString();
var len
= b
.length
;
if (len
<= 3) {
return b
;
}
var r
= len
% 3;
return r
> 0
? b
.slice(0, r
) +
',' +
b
.slice(r
, len
)
.match(/\d{3}/g)
.join(',')
: b
.slice(r
, len
)
.match(/\d{3}/g)
.join(',');
},
有个小小的bug 不过不知道怎么修改了,但是也能用。。。
转载请注明原文地址:https://tech.qufami.com/read-10527.html