Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a trailing zero to a price with jQuery

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?

like image 857
Nathan Pitman Avatar asked Mar 12 '10 13:03

Nathan Pitman


People also ask

What is an example of trailing zero?

The number of trailing zeros in a non-zero base-b integer n equals the exponent of the highest power of b that divides n. For example, 14000 has three trailing zeros and is therefore divisible by 1000 = 103, but not by 104. This property is useful when looking for small factors in integer factorization.

What is trailing leading zero?

However, in decimal fractions strictly between −1 and 1, the leading zeros digits between the decimal point and the first nonzero digit are necessary for conveying the magnitude of a number and cannot be omitted, while trailing zeros – zeros occurring after the decimal point and after the last nonzero digit – can be ...

How do you add a trailing zero to a string in Python?

To add trailing zeros to a string in Python, the easiest way is with the + operator. You can also use the Python string ljust() function to add trailing zeros to a string. One last way is with the format() function. When working with strings, the ability to easily modify the values of the variables easily is valuable.


2 Answers

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.

like image 122
rosscj2533 Avatar answered Sep 18 '22 09:09

rosscj2533


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

var n = 258.2; n.toFixed (2);  // returns 258.20 
like image 40
dbrown0708 Avatar answered Sep 22 '22 09:09

dbrown0708