Question

How to add a trailing zero to a price?

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 latter case, I need to add the trailing zero. How would I go about doing this?

 45  51592  45
1 Jan 1970

Solution

 114

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.

2010-03-12

Solution

 14

Javascript has a function - toFixed - that should do what you want ... no JQuery needed.

var n = 258.2;
n.toFixed (2);  // returns 258.20
2010-03-12