js金额格式化

格式化金额

// s为传入的字符串,dot为金额每隔三位用","或" "间隔
function formatMoney(s,dot) {     
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";   
   var l = s.split(".")[0].split("").reverse(),   
   r = s.split(".")[1];   
   t = "";   
   for(i = 0; i < l.length; i ++ )   {   
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? dot : "");   
   }   
   return t.split("").reverse().join("") + "." + r;   
} 
console.log(formatMoney("12345.605910", ' '))
//  12 345.61

重置格式化金额

function reSetmoney(s) {   
   return parseFloat(s.replace(/[^\d\.-]/g, ""));   
}
console.log(reSet(12 345.61))
// 12345.61
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容