Number
extends NumberFormat
in package
Table of Contents
Properties
- $number : string
- $precision : int
Methods
- __construct() : mixed
- add() : $this
- Adds a number or multiple numbers to the "base" number.
- asFloat() : float
- Returns the number as a float.
- asInteger() : int
- Returns the number as a rounded integer with optional rounding mode.
- asString() : string
- Returns the number instance as a string with optional decimal places and thousands separator formatting.
- decreaseByPercentage() : $this
- Decreases the "base" number by the specified percentage.
- divide() : $this
- Divides the "base" number by a number or multiple numbers.
- increaseByPercentage() : $this
- Increases the "base" number by the specified percentage.
- isEqualTo() : bool
- Determines whether the "base" number is equal to the specified comparator number.
- isGreaterThan() : bool
- Determines whether the "base" number is greater than the specified comparator number.
- isGreaterThanOrEqualTo() : bool
- Determines whether the "base" number is greater than or equal to the specified comparator number.
- isLessThan() : bool
- Determines whether the "base" number is less than the specified comparator number.
- isLessThanOrEqualTo() : bool
- Determines whether the "base" number is less than or equal to the specified comparator number.
- isZero() : bool
- Determines whether the "base" number is zero.
- modulus() : $this
- Calculates the modulus (remainder) when dividing a number by the "base" number.
- multiply() : $this
- Multiplies the "base" number by a number or multiple numbers.
- raiseToPower() : $this
- Raises the "base" number to the power of the specified exponent number.
- raiseToPowerReduceByModulus() : $this
- Raises the "base" number to the power of the specified exponent number and reduces by the modulus (remainder) divisor number.
- roundToDecimalPlaces() : $this
- Rounds "base" number to the specified number of decimal places. Note: this method does not format to the specified number of decimal places, to do so use the `asString` method. * @param int $decimalPlaces the number of decimal places to round to
- squareRoot() : $this
- Calculates the square root of the "base" number.
- subtract() : $this
- Subtracts a number or multiple numbers from the "base" number.
- castNumberToString() : string
Properties
$number
protected
string
$number
$precision read-only
protected
int
$precision
= 24
Methods
__construct()
public
__construct([int|float|string $number = 0 ][, int $precision = 24 ]) : mixed
Parameters
- $number : int|float|string = 0
-
(Optional) The "base" number. Default: 0
- $precision : int = 24
-
(Optional) The number of digits after the decimal place in the result. Default: 24
Tags
add()
Adds a number or multiple numbers to the "base" number.
public
add(Number|int|float|string ...$number) : $this
Parameters
- $number : Number|int|float|string
-
The number or numbers to add to the "base" number.
Return values
$thisasFloat()
Returns the number as a float.
public
asFloat() : float
Return values
float —the number formatted as a float (decimal)
asInteger()
Returns the number as a rounded integer with optional rounding mode.
public
asInteger([int $roundingMode = PHP_ROUND_HALF_UP ]) : int
Parameters
- $roundingMode : int = PHP_ROUND_HALF_UP
-
(Optional) The rounding method defined by PHP internal maths constants [PHP_ROUND_HALF_UP (1) | PHP_ROUND_HALF_DOWN (2) | PHP_ROUND_HALF_EVEN (3) | PHP_ROUND_HALF_ODD (4)]. Default: PHP_ROUND_HALF_UP (1)
Return values
int —the number formatted as an integer (rounded up by default)
asString()
Returns the number instance as a string with optional decimal places and thousands separator formatting.
public
asString([int|null $decimalPlaces = null ][, string $thousandsSeparator = '' ]) : string
Parameters
- $decimalPlaces : int|null = null
-
(Optional) The number of decimal places to return (note: this will not round the number, for rounding use the roundToDecimalPlaces method)
- $thousandsSeparator : string = ''
-
(Optional) Thousands separator. Default: empty string (i.e. none)
Tags
Return values
string —the number formatted as a string
decreaseByPercentage()
Decreases the "base" number by the specified percentage.
public
decreaseByPercentage(Number|int|float|string $percent) : $this
Parameters
- $percent : Number|int|float|string
-
the percentage to decrease the "base" number by
Return values
$thisdivide()
Divides the "base" number by a number or multiple numbers.
public
divide(Number|int|float|string ...$number) : $this
Parameters
- $number : Number|int|float|string
-
The number or numbers to divide by the "base" number.
Tags
Return values
$thisincreaseByPercentage()
Increases the "base" number by the specified percentage.
public
increaseByPercentage(Number|int|float|string $percent) : $this
Parameters
- $percent : Number|int|float|string
-
the percentage to increase the "base" number by
Return values
$thisisEqualTo()
Determines whether the "base" number is equal to the specified comparator number.
public
isEqualTo(Number|int|float|string $number) : bool
Parameters
- $number : Number|int|float|string
-
the comparator number
Return values
bool —returns true when the "base" number is equal to the comparator number, or false when the "base" number is less than or greater than the comparator number
isGreaterThan()
Determines whether the "base" number is greater than the specified comparator number.
public
isGreaterThan(Number|int|float|string $number) : bool
Parameters
- $number : Number|int|float|string
-
the comparator number
Return values
bool —returns true when the "base" number is greater than the comparator number, or false when the "base" number is less than or equal to the comparator number
isGreaterThanOrEqualTo()
Determines whether the "base" number is greater than or equal to the specified comparator number.
public
isGreaterThanOrEqualTo(Number|int|float|string $number) : bool
Parameters
- $number : Number|int|float|string
-
the comparator number
Return values
bool —returns true when the "base" number is greater than or equal to the comparator number, or false when the "base" number is less than the comparator number
isLessThan()
Determines whether the "base" number is less than the specified comparator number.
public
isLessThan(Number|int|float|string $number) : bool
Parameters
- $number : Number|int|float|string
-
the comparator number
Return values
bool —returns true when the "base" number is less than the comparator number, or false when the "base" number is greater than or equal to the comparator number
isLessThanOrEqualTo()
Determines whether the "base" number is less than or equal to the specified comparator number.
public
isLessThanOrEqualTo(Number|int|float|string $number) : bool
Parameters
- $number : Number|int|float|string
-
the comparator number
Return values
bool —returns true when the "base" number is less than or equal to the comparator number, or false when the "base" number is greater than the comparator number
isZero()
Determines whether the "base" number is zero.
public
isZero() : bool
Return values
bool —returns true when the "base" number is equal to zero, or false when the "base" number is less than or greater than zero
modulus()
Calculates the modulus (remainder) when dividing a number by the "base" number.
public
modulus(Number|int|float|string $divisorNumber) : $this
Parameters
- $divisorNumber : Number|int|float|string
-
the divisor number when calculating the modulus (remainder) when dividing by the "base" number
Return values
$thismultiply()
Multiplies the "base" number by a number or multiple numbers.
public
multiply(Number|int|float|string ...$number) : $this
Parameters
- $number : Number|int|float|string
-
The number or numbers to multiple by the "base" number.
Return values
$thisraiseToPower()
Raises the "base" number to the power of the specified exponent number.
public
raiseToPower(Number|int|float|string $exponentNumber) : $this
Parameters
- $exponentNumber : Number|int|float|string
-
the exponent ("power to") number to raise the "base" number by
Tags
Return values
$thisraiseToPowerReduceByModulus()
Raises the "base" number to the power of the specified exponent number and reduces by the modulus (remainder) divisor number.
public
raiseToPowerReduceByModulus(Number|int|float|string $exponentNumber, Number|int|float|string $divisorNumber) : $this
Parameters
- $exponentNumber : Number|int|float|string
-
the exponent ("power to") number to raise the "base" number by
- $divisorNumber : Number|int|float|string
-
the divisor number when calculating the modulus (remainder) when dividing by the "base" number
Tags
Return values
$thisroundToDecimalPlaces()
Rounds "base" number to the specified number of decimal places. Note: this method does not format to the specified number of decimal places, to do so use the `asString` method. * @param int $decimalPlaces the number of decimal places to round to
public
roundToDecimalPlaces(int $decimalPlaces[, int $roundingMode = PHP_ROUND_HALF_UP ]) : $this
Parameters
- $decimalPlaces : int
- $roundingMode : int = PHP_ROUND_HALF_UP
-
(Optional) The rounding method defined by PHP internal maths constants [PHP_ROUND_HALF_UP (1) | PHP_ROUND_HALF_DOWN (2) | PHP_ROUND_HALF_EVEN (3) | PHP_ROUND_HALF_ODD (4)]. Default: PHP_ROUND_HALF_UP (1)
Tags
Return values
$thissquareRoot()
Calculates the square root of the "base" number.
public
squareRoot() : $this
Return values
$thissubtract()
Subtracts a number or multiple numbers from the "base" number.
public
subtract(Number|int|float|string ...$number) : $this
Parameters
- $number : Number|int|float|string
-
The number or numbers to subtract from the "base" number.
Return values
$thiscastNumberToString()
protected
castNumberToString(self|int|float|string $number) : string
Parameters
- $number : self|int|float|string