Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 777
codemaker Avatar asked Jan 27 '16 12:01

codemaker


1 Answers

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)));
});
like image 85
Rory McCrossan Avatar answered Sep 20 '22 07:09

Rory McCrossan