codemaker
codemaker

Reputation: 53

Round up number with decimals

I have the following output numbers from prices of products:

<span class="amount">77.08</span>
<span class="amount">18.18</span>
<span class="amount">20.25</span>

I want to round them up always no matter what price is, so the result in this case would be:

<span class="amount">78</span>
<span class="amount">19</span>
<span class="amount">21</span>

I tried with this code but it just removes the whole number:

jQuery('.amount').each(function () {
    jQuery(this).html(Math.round(parseFloat(jQuery(this).html())));
});

Any idea where is the problem?

Upvotes: 5

Views: 172

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337713

Use Math.ceil() instead of Math.round():

jQuery(this).html(Math.ceil(parseFloat(jQuery(this).html())));

Example fiddle

Also note that you can provide a function to html() to tidy the code a little:

$(this).html(function(i, html) {
    return Math.ceil(parseFloat(html)));
});

Upvotes: 9

Related Questions