Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly format currency using jquery?

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

like image 574
ninjasense Avatar asked Feb 18 '11 16:02

ninjasense


People also ask

What is the format of jQuery?

The jQuery Format plugin enables the formatting and parsing of dates and numbers. It's a client-side alternative of the popular SimpleDateFormat and NumberFormat APIs.

How do you format numbers in JavaScript?

JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.


2 Answers

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()); 
like image 184
Melu Avatar answered Sep 19 '22 19:09

Melu


JQUERY FORMATCURRENCY PLUGIN
http://code.google.com/p/jquery-formatcurrency/

like image 30
Robert Harvey Avatar answered Sep 17 '22 19:09

Robert Harvey