How can I correctly format currency using jquery?

前端 未结 7 1868
故里飘歌
故里飘歌 2020-12-13 01:16

I do not need a mask, but I need something that will format currency(in all browsers) and not allow for any letters or special char\'s to be typed. Thanks for the help

7条回答
  •  天命终不由人
    2020-12-13 02:07

    Another option (If you are using ASP.Net razor view) is, On your view you can do

    @String.Format("{0:C}", Model.total)

    This would format it correctly. note (item.total is double/decimal)

    if in jQuery you can also use Regex

    $(".totalSum").text('$' + parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
    

提交回复
热议问题