A fluent interface for interacting with numbers, providing immutable arithmetic, rounding, formatting, and inspection methods.
Returns
Utility
Get the absolute value of the number.
echo (new Utility(-5))->abs()->value();
// 5Returns
Utility
Add a number to the current value.
echo (new Utility(5))->add(10)->value();
// 15Returns
Utility
Round up the number using ceil.
echo (new Utility(4.3))->ceil()->value();
// 5Returns
Utility
Constrain the number between a minimum and maximum value.
echo (new Utility(15))->clamp(1, 10)->value();
// 10
echo (new Utility(-5))->clamp(0, 100)->value();
// 0Returns
Utility
Divide the current value by the given number.
echo (new Utility(25))->divide(5)->value();
// 5Returns
array
Get the factors of the current number.
(new Utility(12))->factors();
// [1, 2, 3, 4, 6, 12]Returns
Utility
Round down the value using floor.
echo (new Utility(4.3))->floor()->value();
// 4Returns
bool
Check if the number is between two values (inclusive).
(new Utility(5))->isBetween(1, 10);
// true
(new Utility(11))->isBetween(1, 10);
// falseReturns
bool
Check if the number is an even integer. Returns false for floats.
(new Utility(4))->isEven();
// trueReturns
bool
Check if the number is negative. Throws IsZeroException for zero.
(new Utility(-5))->isNegative();
// trueReturns
bool
Check if the number is an odd integer. Returns false for floats.
(new Utility(3))->isOdd();
// trueReturns
bool
Check if the number is positive. Throws IsZeroException for zero.
(new Utility(5))->isPositive();
// trueReturns
bool
Check if the number is zero.
(new Utility(0))->isZero();
// trueReturns
Utility
Get the order of magnitude of the number.
echo (new Utility(1000))->magnitude()->value();
// 3Returns
Utility
Static factory method to create a new Utility instance.
$n = Utility::make(42);Returns
Utility
Subtract a value from the number.
echo (new Utility(14))->minus(7)->value();
// 7Returns
Utility
Get the remainder after division. Throws DivisionByZeroError for zero divisor.
echo (new Utility(10))->modulo(3)->value();
// 1Returns
Utility
Multiply the number by the given value.
echo (new Utility(7))->multiply(7)->value();
// 49Returns
string
Get the ordinal suffix of a number (st, nd, rd, th).
echo (new Utility(1))->ordinal();
// stReturns
string
Pad the number on the left to a given length.
echo (new Utility(1))->padLeft(3);
// 001Returns
string
Pad the number on the right to a given length.
echo (new Utility(1))->padRight(3);
// 100Returns
Utility
Raise the number to a given exponent.
echo (new Utility(5))->power(3)->value();
// 125Returns
Utility
Round down the number to a given precision.
echo (new Utility(4.5))->roundDown()->value();
// 4
echo (new Utility(4.2345))->roundDown(2)->value();
// 4.23Returns
Utility
Round up the number to a given precision.
echo (new Utility(4.5))->roundUp()->value();
// 5
echo (new Utility(4.2345))->roundUp(3)->value();
// 4.235Returns
Utility
Get the square root of the number. Throws InvalidNumberException for negative numbers.
echo (new Utility(9))->sqrt()->value();
// 3Returns
string
Get the type name of the current value.
echo (new Utility(7))->type();
// int
echo (new Utility(4.9))->type();
// floatReturns
int|float
Get the current numeric value.
echo (new Utility(42))->value();
// 42Returns
string
Get the number with its ordinal suffix.
echo (new Utility(1))->withOrdinal();
// 1st
echo (new Utility(2))->withOrdinal(' ');
// 2 nd