Reputation: 53
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
Reputation: 337713
Use Math.ceil()
instead of Math.round()
:
jQuery(this).html(Math.ceil(parseFloat(jQuery(this).html())));
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