toolkit

Num

Number helpers: format, round, abs, clamp and friends.

Num:: Phlat\Toolkit\Num 15 methods
  • isNumeric Whether the value is a number or a numeric string.

Formatting 4

  • format Formats a number with grouped thousands and decimal point.
  • currency Formats a value as currency.
  • percent Formats a value as a percentage.
  • bytes Formats a byte count as a human-readable size string.

Math 10

  • round Rounds to $precision decimals.
  • floor Rounds down.
  • ceil Rounds up.
  • intdiv Integer division, rounded toward zero.
  • abs The absolute value.
  • clamp Clamps a value between $min and $max.
  • min The smallest of the values.
  • max The largest of the values.
  • random Cryptographically secure random integer.
  • baseConvert Converts a number string from one base to another (e.g. for compact cache tokens).

Methods

Num::isNumeric(mixed $value): bool

Whether the value is a number or a numeric string.

Parameters (1)
$value mixed

Returns bool

phlat/src/Toolkit/Num.php:15

Formatting

Num::format(int|float $number, int $decimals = 0, string $point = '.', string $thousands = ','): string

Formats a number with grouped thousands and decimal point.

Parameters (4)
$number int|float
$decimals int = 0
$point string = '.'
$thousands string = ','

Returns string

phlat/src/Toolkit/Num.php:23
Num::currency(int|float $amount, string $symbol = '$', int $decimals = 2): string

Formats a value as currency.

Parameters (3)
$amount int|float
$symbol string = '$'
$decimals int = 2

Returns string

phlat/src/Toolkit/Num.php:29
Num::percent(int|float $value, int $decimals = 1): string

Formats a value as a percentage.

Parameters (2)
$value int|float
$decimals int = 1

Returns string

phlat/src/Toolkit/Num.php:35
Num::bytes(int $bytes): string

Formats a byte count as a human-readable size string.

Parameters (1)
$bytes int

Returns string

phlat/src/Toolkit/Num.php:41

Math

Num::round(int|float $value, int $precision = 0): int|float

Rounds to $precision decimals.

Parameters (2)
$value int|float
$precision int = 0

Returns int|float

phlat/src/Toolkit/Num.php:59
Num::floor(int|float $value): int|float

Rounds down.

Parameters (1)
$value int|float

Returns int|float

phlat/src/Toolkit/Num.php:65
Num::ceil(int|float $value): int|float

Rounds up.

Parameters (1)
$value int|float

Returns int|float

phlat/src/Toolkit/Num.php:71
Num::intdiv(int $dividend, int $divisor): int

Integer division, rounded toward zero.

Parameters (2)
$dividend int
$divisor int

Returns int

phlat/src/Toolkit/Num.php:77

abs

Num::abs(int|float $value): int|float

The absolute value.

Parameters (1)
$value int|float

Returns int|float

phlat/src/Toolkit/Num.php:83
Num::clamp(int|float $value, int|float $min, int|float $max): int|float

Clamps a value between $min and $max.

Parameters (3)
$value int|float
$min int|float
$max int|float

Returns int|float

phlat/src/Toolkit/Num.php:89

min

Num::min(int|float ...$values): int|float

The smallest of the values.

Parameters (1)
...$values int|float

Returns int|float

phlat/src/Toolkit/Num.php:95

max

Num::max(int|float ...$values): int|float

The largest of the values.

Parameters (1)
...$values int|float

Returns int|float

phlat/src/Toolkit/Num.php:101
Num::random(int $min = 0, int $max = PHP_INT_MAX): int

Cryptographically secure random integer.

Parameters (2)
$min int = 0
$max int = PHP_INT_MAX

Returns int

phlat/src/Toolkit/Num.php:107
Num::baseConvert(string $number, int $from, int $to): string

Converts a number string from one base to another (e.g. for compact cache tokens).

Parameters (3)
$number string
$from int
$to int

Returns string

phlat/src/Toolkit/Num.php:113