May 11, 2007

JavaScript: precision_round(), dollar_value()

Filed under: Tech — Mike @ 1:34 pm

More geeky shit today. JavaScript has a Math.round() function, but no convenient way to achieve a precision round. So for example, if you have a value of 8.942 and you want to round it to 8.94, you have to do some more math in your code. This handy little function lets you do that a little more easily.

Building on that, here’s another function I wrote that’s pretty handy for expressing numbers as formatted dollar values, complete with commas for values above $1,000. You can call this function one of three ways:

var v = dollar_value(); // $0.00

var v = dollar_value(3.4); // $3.40

var v = dollar_value(3.4, ‘€’) // €3.40

Here’s the dollar_value() function.  Note you need the precision_round() function for this to work.

No Comments »

No comments yet.

RSS feed for comments on this post. | TrackBack URI

Leave a comment

You must be logged in to post a comment.