77.08 18.18...">

Decimal round number

I have the following output numbers from product prices:

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

I always want to round them regardless of price, so the result in this case will 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 integer:

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

Any idea where the problem is?

+5
source share
1 answer

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

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

Script example

Also note that you can provide the html() function to streamline the code a bit:

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

Source: https://habr.com/ru/post/1241611/


All Articles