货币价值的JavaScript?

时间:2015-09-29 13:55:05

标签: javascript

我有这个功能来格式化货币价值:

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;   
}

当我输入一个数字11,1时,此函数会假设值:

0,01

0,11

如果以正确的方式进行值对齐,当我输入数字11,1时,此函数可能会采用左侧的值:

1,00

1,10

0 个答案:

没有答案