API reference
Every public method of BigNumber and Utils, grouped by purpose.
Every BigNumber method that returns self returns a new instance. The class is PHPMath\BigNumber\BigNumber. The helper class is PHPMath\BigNumber\Utils. All thrown exceptions implement PHPMath\BigNumber\Exception\BigNumberException and extend PHP's InvalidArgumentException, see Errors and exceptions. A value is a string|int|float|BigNumber. The library has no named constructors.
Construction and output#
Create a number, read its value and scale, and change the scale.
__construct()#
public function __construct(string|int|float|self $value = 0, private int $scale = 10)
Creates a number. The value is normalised and truncated to the scale.
- Parameters:
$valuethe value,$scalethe number of decimals to keep (0 or higher) - Throws:
InvalidValueExceptionif the value is not numeric,InvalidScaleExceptionif the scale is negative
getValue()#
public function getValue(): string
Returns the number as a string with exactly as many decimals as the scale.
- Returns:
numeric-string
withScale()#
public function withScale(int $scale): self
Returns a copy of the number with another scale. A smaller scale truncates decimals.
- Parameters:
$scalethe scale of the new number - Returns:
BigNumber - Throws:
InvalidScaleExceptionif the scale is negative
round()#
public function round(int $scale): self
Returns a copy of the number, rounded to the given scale. A half rounds away from zero, like PHP's round(). Unlike withScale(), which cuts digits off, this looks at the digits that are removed. A scale that is equal to or larger than the current scale only changes the scale.
- Parameters:
$scalethe scale of the new number - Returns:
BigNumber - Throws:
InvalidScaleExceptionif the scale is negative
toString()#
public function toString(): string
Returns the same string as getValue().
- Returns:
string
__toString()#
public function __toString(): string
Returns the same string as getValue(). This makes the class Stringable.
- Returns:
string
Arithmetic#
Operations that return a new number, truncated to the scale of the number they are called on.
add()#
public function add(string|int|float|self $value): self
Adds a value.
- Parameters:
$valuethe value to add - Returns:
BigNumber - Throws:
InvalidValueExceptionif the value is not numeric
subtract()#
public function subtract(string|int|float|self $value): self
Subtracts a value.
- Parameters:
$valuethe value to subtract - Returns:
BigNumber - Throws:
InvalidValueExceptionif the value is not numeric
multiply()#
public function multiply(string|int|float|self $value): self
Multiplies by a value.
- Parameters:
$valuethe value to multiply with - Returns:
BigNumber - Throws:
InvalidValueExceptionif the value is not numeric
divide()#
public function divide(string|int|float|self $value): self
Divides by a value. The result is truncated toward zero.
- Parameters:
$valuethe divisor - Returns:
BigNumber - Throws:
InvalidValueExceptionif the value is not numeric,DivisionByZeroExceptionif the divisor is zero
mod()#
public function mod(string|int|float|self $value): self
Returns the remainder of a division. The result has the sign of the dividend.
- Parameters:
$valuethe divisor - Returns:
BigNumber - Throws:
InvalidValueExceptionif the value is not numeric,DivisionByZeroExceptionif the divisor is zero
pow()#
public function pow(int|string $exponent): self
Raises the number to an integer power.
- Parameters:
$exponentan int, or a string of digits. A negative exponent is accepted as an int or as a string, with the same result. - Returns:
BigNumber - Throws:
InvalidExponentExceptionif the exponent is not an integer
sqrt()#
public function sqrt(): self
Returns the square root.
- Returns:
BigNumber - Throws:
NegativeValueExceptionif the number is negative
Utils: conversion#
Static helpers that convert values to plain strings and integers between bases.
Utils::toNumericString()#
public static function toNumericString(string|int|float|BigNumber $value): string
Converts a value to a plain decimal string without exponent, leading zeros or trailing decimal zeros. A BigNumber gives its value string unchanged.
- Parameters:
$valuethe value to convert - Returns:
numeric-string - Throws:
InvalidValueExceptionif the value is not numeric
Utils::convertExponentialToString()#
public static function convertExponentialToString(float|string $value): string
Writes a float as a plain decimal string without exponent. A string is returned unchanged.
- Parameters:
$valuethe float or string - Returns:
string
Utils::convertToBase10()#
public static function convertToBase10(string|int|float|BigNumber $number, int $fromBase): string
Converts an integer from another base to base 10. A fractional part is discarded.
- Parameters:
$numberthe number,$fromBasethe base it is written in (2 to 36) - Returns:
string - Throws:
InvalidBaseExceptionif the base is out of range,InvalidValueExceptionif the number has a digit that is invalid for the base
Utils::convertBase()#
public static function convertBase(string|int|float|BigNumber $number, int $fromBase, int $toBase): string
Converts an integer from one base to another. A fractional part is discarded. Letters in the result are uppercase.
- Parameters:
$numberthe number,$fromBasethe source base,$toBasethe target base (both 2 to 36) - Returns:
string - Throws:
InvalidBaseExceptionif a base is out of range,InvalidValueExceptionif the number has a digit that is invalid for the source base
Utils: other#
A constant with the digits of the supported bases.
Utils::ALPHABET#
public const string ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
The digits used for bases 2 to 36.
- Returns:
string