How to add a trailing zero to a price with jQuery

Nathan Pitman picture Nathan Pitman · Mar 12, 2010 · Viewed 40.4k times · Source

So I have a script which returns a price for a product. However the price may or may not include trailing zeros so sometimes I might have:

258.22

and other times I might have

258.2

In the later case I need to add the trailing zero with jQuery. How would I go about doing this?

Answer

rosscj2533 picture rosscj2533 · Mar 12, 2010

You can use javascript's toFixed method (source), you don't need jQuery. Example:

var number = 258.2;    
var rounded = number.toFixed(2); // rounded = 258.20

Edit: Electric Toolbox link has succumbed to linkrot and blocks the Wayback Machine so there is no working URL for the source.