Since: 8.4

final class Number implements Stringable (View source)

A class for an arbitrary precision number. These objects support overloaded arithmetic and comparison operators.

Properties

numeric-string $value
int $scale

Methods

__construct(string|int $num)

Creates a BcMath\Number object

add(Number|string|int $num, int|null $scale = null)

Adds an arbitrary precision number

sub(Number|string|int $num, int|null $scale = null)

Subtracts an arbitrary precision number

mul(Number|string|int $num, int|null $scale = null)

Multiplies an arbitrary precision number

div(Number|string|int $num, int|null $scale = null)

Divides by an arbitrary precision number

mod(Number|string|int $num, int|null $scale = null)

Gets the modulus of an arbitrary precision number

array
divmod(Number|string|int $num, int|null $scale = null)

Gets the quotient and modulus of an arbitrary precision number

powmod(Number|string|int $exponent, Number|string|int $modulus, int|null $scale = null)

Raises an arbitrary precision number, reduced by a specified modulus

pow(Number|string|int $exponent, int|null $scale = null)

Raises an arbitrary precision number

sqrt(int|null $scale = null)

Gets the square root of an arbitrary precision number

floor()

Rounds down an arbitrary precision number

ceil()

Rounds up an arbitrary precision number

round(int $precision = 0, RoundingMode $mode = \RoundingMode::HalfAwayFromZero)

Rounds an arbitrary precision number

int
compare(Number|string|int $num, int|null $scale = null)

Compares two arbitrary precision numbers

string
__toString()

Converts BcMath\Number to string

array
__serialize()

Serializes a BcMath\Number object

void
__unserialize(array $data)

Deserializes a data parameter into a BcMath\Number object

Details

__construct(string|int $num)

Creates a BcMath\Number object

Creates a BcMath\Number object from an int or string value.

Parameters

string|int $num

An int or string value. If num is a int, the BcMath\Number::scale is always set to 0. If num is a string, it must be a valid number, and the BcMath\Number::scale is automatically set by parsing the string.

Exceptions

ValueError

Number add(Number|string|int $num, int|null $scale = null)

Adds an arbitrary precision number

Adds $this and num.

Parameters

Number|string|int $num

The value to add.

int|null $scale

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

Return Value

Number

Exceptions

ValueError

Number sub(Number|string|int $num, int|null $scale = null)

Subtracts an arbitrary precision number

Subtracts num from $this.

Parameters

Number|string|int $num

The value to subtract.

int|null $scale

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

Return Value

Number

Exceptions

ValueError

Number mul(Number|string|int $num, int|null $scale = null)

Multiplies an arbitrary precision number

Multiplies $this by num.

Parameters

Number|string|int $num

The multiplier.

int|null $scale

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

Return Value

Number

Exceptions

ValueError

Number div(Number|string|int $num, int|null $scale = null)

Divides by an arbitrary precision number

Divides $this by num.

Parameters

Number|string|int $num

The divisor.

int|null $scale

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

Return Value

Number

Exceptions

DivisionByZeroError
ValueError

Number mod(Number|string|int $num, int|null $scale = null)

Gets the modulus of an arbitrary precision number

Gets the remainder of dividing $this by num. Unless num is 0, the result has the same sign as $this.

Parameters

Number|string|int $num

The divisor.

int|null $scale

Return Value

Number

Exceptions

DivisionByZeroError
ValueError

array divmod(Number|string|int $num, int|null $scale = null)

Gets the quotient and modulus of an arbitrary precision number

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

Parameters

Number|string|int $num

The divisor.

int|null $scale

Return Value

array

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.

Exceptions

DivisionByZeroError
ValueError

Number powmod(Number|string|int $exponent, Number|string|int $modulus, int|null $scale = null)

Raises an arbitrary precision number, reduced by a specified modulus

Use the fast-exponentiation method to raise $this to the power exponent with respect to the modulus modulus.

Parameters

Number|string|int $exponent

The exponent, as an non-negative and integral (i.e. the scale has to be zero).

Number|string|int $modulus

The modulus, as an integral (i.e. the scale has to be zero).

int|null $scale

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

Return Value

Number

Exceptions

DivisionByZeroError
ValueError

Number pow(Number|string|int $exponent, int|null $scale = null)

Raises an arbitrary precision number

Raises $this to the exponent power.

Parameters

Number|string|int $exponent

The exponent. Must be a value with no fractional part. The valid range of the exponent is platform specific, but it is at least -2147483648 to 2147483647.

int|null $scale

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

Return Value

Number

Exceptions

DivisionByZeroError
ValueError

Number sqrt(int|null $scale = null)

Gets the square root of an arbitrary precision number

Return the square root of $this.

Parameters

int|null $scale

Return Value

Number

Exceptions

ValueError

Number floor()

Rounds down an arbitrary precision number

Returns the next highest integer value by rounding down $this if necessary.

Return Value

Number

Returns the result as a new BcMath\Number object. The BcMath\Number::scale of the result is always 0.

Number ceil()

Rounds up an arbitrary precision number

Returns the next highest integer value by rounding up $this if necessary.

Return Value

Number

Returns the result as a new BcMath\Number object. The BcMath\Number::scale of the result is always 0.

Number round(int $precision = 0, RoundingMode $mode = \RoundingMode::HalfAwayFromZero)

Rounds an arbitrary precision number

Returns the rounded value of $this to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Parameters

int $precision
RoundingMode $mode

Return Value

Number

Exceptions

ValueError

int compare(Number|string|int $num, int|null $scale = null)

Compares two arbitrary precision numbers

Compare two arbitrary precision numbers. This method behaves similar to the spaceship operator.

Parameters

Number|string|int $num

The value to be compared to.

int|null $scale

Specify the scale to use for comparison. If null, all digits are used in the comparison.

Return Value

int

Returns -1, 0, or 1

Exceptions

ValueError

string __toString()

Converts BcMath\Number to string

Return Value

string

Returns string representation of the object that implements this interface (and/or "__toString" magic method).

array __serialize()

Serializes a BcMath\Number object

Serializes $this.

Return Value

array

void __unserialize(array $data)

Deserializes a data parameter into a BcMath\Number object

Parameters

array $data

The serialized data parameter as an associative array

Return Value

void

Exceptions

ValueError