Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  58] [ 2]  / answers: 1 / hits: 103780  / 15 Years ago, tue, april 7, 2009, 12:00:00

I'm trying to format various numbers on my page. These numbers either represent a price, a change in price, or a percentage. I know Javascript has functions to limit the number of decimal places, but is there any support for other types of formatting, such as grouping numbers with commas, controlling whether or not the +/- is shown, etc? Here's what I have so far:



var FORMATTER = {
price : function(value) { return '$' + value.toFixed(2); },
pricePer : function(value) { return (value * 100).toFixed(2) + '%'; },
priceChg : function(value) { return (value >= 0 ? '+' : '-') + '$' + Math.abs(value).toFixed(2); }
};


It works OK, but it'd like to add commas to the 'price' formatter, and you can see that there's a hack in the 'priceChg' formatter where I try to move the +/- sign in front of the '$' sign.



Basically, I'm hoping there is some library out there (jQuery is OK) that emulates Java's DecimalFormat class.


More From » jquery

 Answers
26

There's the NUMBERFORMATTER jQuery plugin, details below:



https://code.google.com/p/jquery-numberformatter/



From the above link:




This plugin is a NumberFormatter
plugin. Number formatting is likely
familiar to anyone who's worked with
server-side code like Java or PHP and
who has worked with
internationalization.




EDIT: Replaced the link with a more direct one.


[#99735] Thursday, April 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emmaleet

Total Points: 203
Total Questions: 107
Total Answers: 98

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;