array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'tr', ), 'this' => array ( 0 => 'bcmath-number.divmod.php', 1 => 'BcMath\\Number::divmod', 2 => 'Gets the quotient and modulus of an arbitrary precision number', ), 'up' => array ( 0 => 'class.bcmath-number.php', 1 => 'BcMath\\Number', ), 'prev' => array ( 0 => 'bcmath-number.div.php', 1 => 'BcMath\\Number::div', ), 'next' => array ( 0 => 'bcmath-number.floor.php', 1 => 'BcMath\\Number::floor', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/bc/bcmath/number/divmod.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

BcMath\Number::divmod

(PHP 8 >= 8.4.0)

BcMath\Number::divmodGets the quotient and modulus of an arbitrary precision number

Açıklama

public BcMath\Number::divmod(BcMath\Number|string|int $num, ?int $scale = null): array

Gets the quotient and remainder of dividing $this by num.

Bağımsız Değişkenler

num
The divisor.
scale
BcMath\Number::scale explicitly specified for calculation results. If null, the BcMath\Number::scale of the calculation result will be set automatically.

Dönen Değerler

Returns an indexed array where the first element is the quotient as a new BcMath\Number object and the second element is the remainder as a new BcMath\Number object.

The quotient is always an integer value, so BcMath\Number::scale of the quotient will always be 0, regardless of whether explicitly specify scale.

If scale is explicitly specified, BcMath\Number::scale of the remainder will be the specified value. When the BcMath\Number::scale of the result's remainder object is automatically set, the greater BcMath\Number::scale of the two numbers used for modulus operation is used.

That is, if the BcMath\Number::scales of two values are 2 and 5 respectively, the BcMath\Number::scale of the remainder will be 5.

Hatalar/İstisnalar

This method throws a ValueError in the following cases:

This method throws a DivisionByZeroError exception if num is 0.

Örnekler

Örnek 1 BcMath\Number::divmod() example when scale is not specified

<?php
echo '8.3 / 2.22' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('8')->divmod(new BcMath\Number('2.22'));
var_dump($quot, $rem);

echo
PHP_EOL . '8.3 / 8.3' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('8.3')->divmod('8.3');
var_dump($quot, $rem);

echo
PHP_EOL . '10 / -3' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('10')->divmod(-3);
var_dump($quot, $rem);
?>

Yukarıdaki örneğin çıktısı:

8.3 / 2.22
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(1) "3"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(4) "1.34"
  ["scale"]=>
  int(2)
}

8.3 / 8.3
object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(1) "1"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(3) "0.0"
  ["scale"]=>
  int(1)
}

10 / -3
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(2) "-3"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#1 (2) {
  ["value"]=>
  string(1) "1"
  ["scale"]=>
  int(0)
}

Örnek 2 BcMath\Number::divmod() example of explicitly specifying scale

<?php
echo '8.3 / 2.22' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('8')->divmod(new BcMath\Number('2.22'), 1);
var_dump($quot, $rem);

echo
PHP_EOL . '8.3 / 8.3' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('8.3')->divmod('8.3', 4);
var_dump($quot, $rem);

echo
PHP_EOL . '10 / -3' . PHP_EOL;
[
$quot, $rem] = new BcMath\Number('10')->divmod(-3, 5);
var_dump($quot, $rem);
?>

Yukarıdaki örneğin çıktısı:

8.3 / 2.22
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(1) "3"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.3"
  ["scale"]=>
  int(1)
}

8.3 / 8.3
object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(1) "1"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(6) "0.0000"
  ["scale"]=>
  int(4)
}

10 / -3
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(2) "-3"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#1 (2) {
  ["value"]=>
  string(7) "1.00000"
  ["scale"]=>
  int(5)
}

Ayrıca Bakınız