Divide integer and get integer value

Php

Php Problem Overview


In languages like C or Python, if I divide an integer by an integer, I get an integer:

>>> 8/3
2

But in PHP, if I divide an integer by another integer with /, sometimes I get a float:

php > var_dump(6/3);
int(2)
php > var_dump(8/3);
float(2.6666666666667)

I'd like to do division like in Python or C, so that 8/3 is 2. How can I do that in PHP?

Php Solutions


Solution 1 - Php

use round() function to get integer rounded value.

round(8 / 3); // 3

or

Use floor() function to get integer value

floor(8 / 3); // 2

Solution 2 - Php

In PHP 7, there is intdiv function doing exactly what you want.

Usage:

intdiv(8, 3);

Returns 2.

Solution 3 - Php

There is no integer division operator in PHP. 1/2 yields the float 0.5. The value can be casted to an integer to round it downwards, or the round() function provides finer control over rounding.


var_dump(25/7);           // float(3.5714285714286)

var_dump((int) (25/7));   // int(3)

var_dump(round(25/7));    // float(4)

PhP manual

Solution 4 - Php

use this....

intval(1700000 / 300000 )...

this returns the integer value.

Solution 5 - Php

FOR PHP 7 try intdiv() Function:

> Syntax: int intdiv($dividend, $divisor)

          <?php           
           $dividend = 19; 
           $divisor = 3;  
           echo intdiv($dividend, $divisor); 
           ?>

For Older versions of PHP:

         <?php
     // Convert $total_minutes to hours and minutes.

         $total_minutes = 640;
         $minutes = $total_minutes % 60;
         $hours = ($total_minutes - $minutes) / 60;

         echo "Time taken was $hours hours $minutes minutes";
         ?>

Solution 6 - Php

There are several ways to perform integer division in PHP. The language doesn't have an operator for integer division, but there are several options for rounding the floating point quotient to an integer:

<?php
$pos = 1;
$neg = -1;
$divisor = 2;

// No rounding (float division)
var_dump($pos / $divisor);          //  0.5 (float)
var_dump($neg / $divisor);          // -0.5 (float)

// Round toward zero (like C integer division)
var_dump((int)($pos / $divisor));   //  0 (int)
var_dump((int)($neg / $divisor));   //  0 (int)

// Round half away from zero
var_dump(round($pos / $divisor));   //  1 (float)
var_dump(round($neg / $divisor));   // -1 (float)

// Round down
var_dump(floor($pos / $divisor));   //  0 (float)
var_dump(floor($neg / $divisor));   // -1 (float)

# And on PHP 7 you can round toward zero with intdiv():
var_dump(intdiv($pos, $divisor));   //  0 (int)
var_dump(intdiv($neg, $divisor));   //  0 (int)  Rounded toward zero

On PHP 7 you can use intdiv($p, $q) to directly perform integer division. This is equivalent to (int)($p / $q) on PHP 5.

Solution 7 - Php

(int)(1700000 / 300000);

use type casting.

Solution 8 - Php

You can use the shortform by adding |0 to the end

8/3|0

Solution 9 - Php

For PHP version 7 => intdiv(a,b)

And for versions less than 7 (like 5.6) => (int)floor(abs(a/b))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIsmail SalehView Question on Stackoverflow
Solution 1 - PhpiLaYa ツView Answer on Stackoverflow
Solution 2 - PhpMarek LisýView Answer on Stackoverflow
Solution 3 - PhpS.P.View Answer on Stackoverflow
Solution 4 - PhpbipenView Answer on Stackoverflow
Solution 5 - PhpAamir KalimiView Answer on Stackoverflow
Solution 6 - PhpColin D BennettView Answer on Stackoverflow
Solution 7 - Phpmahipal purohitView Answer on Stackoverflow
Solution 8 - PhpBesiView Answer on Stackoverflow
Solution 9 - Phpsanjay.chouhan180View Answer on Stackoverflow