How can I correctly format currency using jquery?

ninjasense picture ninjasense · Feb 18, 2011 · Viewed 239.3k times · Source

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

Example:

Valid: $50.00
$1,000.53

Not Valid: $w45.00
$34.3r6

Answer

Melu picture Melu · Jun 6, 2013

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

<div>@String.Format("{0:C}", Model.total)</div>

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());