金额格式的转换方法

小结 刘东兵
文章标签: 小结

练习的时候踩了个坑,如何将一串数字转成金额格式。总结一下方法,话不多说代码见真章:

function toMoney(num){
     num=num.toFixed(2) //将数字转成带有2位小数的字符串
     num=parseFloat(num) // 将带有2位小数的字符串转成带有小数的数字
     num=num.toLocaleString(); //将带有2位小数的数字转成金额格式
     return num //返回
}
 console.log("$"+toMoney(1897945.89)) //调用方法 传参数 注意参数只能是数字 打印 $1,897,945.89

方法如上,代码中相应的注释都有。

还能输出{{restrictNumber}}个字符  
  • {{reply.author}}

    {{CommonUtil.formateDate(reply.ac_CommentDate).shortTime}}
  • 回复了{{Comments.author}} :