我有这个功能来格式化货币价值:
function formatMonetary(v){
v = v.replace(/\D/g,"");
v = v.replace(/(\d{1})(\d{15})$/,"$1.$2");
v = v.replace(/(\d{1})(\d{11})$/,"$1.$2");
v = v.replace(/(\d{1})(\d{8})$/,"$1.$2");
v = v.replace(/(\d{1})(\d{5})$/,"$1.$2");
v = v.replace(/(\d{1})(\d{1,2})$/,"$1,$2");
return v;
}
当我输入一个数字1
或1,1
时,此函数会假设值:
0,01
0,11
如果以正确的方式进行值对齐,当我输入数字1
或1,1
时,此函数可能会采用左侧的值:
1,00
1,10