15306

How to round up integer in PHP?

Question:

I want to round up any an integer to the next tens place number.

Some examples to illustrate my point:

-- I have the number 1, I would like this rounded up to 10

-- I have the number 35, I would like this rounded up to 40

-- I have the number 72, I would like this rounded up to 80

-- etc etc

// $category_count's value is 38 for($i = 1; $i <= $category_count; $i++) { if($i % 10 == 0) { echo "<a href=\"?page=$i\">$i;</a>"; } }

The above code outputs 3 links, I need the fourth too.

Answer1:

Your for is ineffective. If you need to modulus 10 your counter, use this code instead :

for($i = 1, $c = ceil($category_count/10); $i <= $c; $i++) { $j = $i * 10; echo "<a href=\"?page=$j\">$j;</a>"; }

Answer2:

mrtsherman is almost right but OP's question needed it to round UP (1 => 10)

// $category_count's value is 38 $loop_limit = ceil($category_count/10); for ($i = 1; $i <= $loop_limit; $i++) { $page = $i * 10; echo "<a href=\"?page={$page}\">{$page}</a>"; }

Answer3:

Edited my answer to point to this question instead. Basically same thing with many good answers.

<a href="https://stackoverflow.com/questions/1619265/round-to-nearest-10" rel="nofollow">How to round up a number to nearest 10?</a>

Answer4:

round(($num/10))*10; // Make sure num is an integer or use (int) to convert string to integer.

This works for me :)

Answer5:

One way to do this is to add 9, then truncate it at the tens place (integer divide by ten, then multiply by ten).

Alternatively, you can add 5 then use the round function with a negative precision:

echo round ($i + 5, -1);

Answer6:

Try the following

ceil($category_count/10)*10

Answer7:

Something like this should work:

(int(i/10) + 1) x 10

Recommend

  • Redirecting Posts on Tumblr to Custom Pages
  • shell: What the means of number of sentence
  • Haskell Aeson to deal with missing data
  • Round up to nearest power of 10
  • Jquery hide first 12 elementes, show next 12 elements
  • python setup.py install changes script interpreter
  • Using Double in Switch Statement
  • Splitting string into groups of specific length
  • Plot ROC curve and calculate AUC in R at specific cutoff info
  • Execution order of expressions in SELECT statement
  • constexpr vs const vs constexpr const
  • Prevent Emacs from modifying the OS X clipboard?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Prolog Query - Trying to understand how this result happens
  • Microsoft Chart Controls for Microsoft .NET Framework 4.0
  • Calling java project from Mathematica
  • PHP file_exists() anomaly
  • How to sort a same column both in asc order and desc order
  • Is mp4 stream able with ffserver?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • IE10 strips out hashtag from the URL
  • mave 3.2 not able to access local nexus instance return 502 code
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • For loop with if condition on multiple R functions
  • Find group of records that match multiple values
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • A cron job substitute?
  • Buffer size for converting unsigned long to string
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal