This library provides a set of utility functions for working with numbers in PHP, including formatting as currency, percentages, ordinals, and more.
Installation#
Install the package using Composer:
composer require friendsofphp/number
Basic Usage#
The Number package provides a single class, Number
, which can be used to format numbers in a variety of ways. Here are some example, with the full reference below.
use FriendsOfPhp\Number\Number;
// Format a number
echo Number::format(1234567.89); // 1,234,567.89
// Spell out a number
echo Number::spell(1234567.89); // one thousand two hundred thirty-four
// Get the ordinal form of a number
echo Number::ordinal(1234567.89); // 42nd
// Format a number as a percentage
echo Number::percentage(1234567.89); // 1%
// Format a number as currency
echo Number::currency(1234567.89, 'EUR'); // €1,234.56
// Format file size
echo Number::fileSize(1234567.89); // 1 KB
// Get a human-readable representation of a number
echo Number::format(1234567.89); // 1 million
// Get the abbreviated form of a number
echo Number::format(1234567.89); // 1M
Full Reference#
Number::format()
#
Format the given number according to the current locale.
Number::format(int|float $number, int|null $precision, int|null $maxPrecision, ?string $locale): string|false
Usage#
echo Number::format(1234567.89); // 1,234,567.89
Number::spell()
#
Spell out the given number in the given locale.
Number::spell(int|float $number, ?string $locale): string
Usage#
echo Number::spell(1234); // one thousand two hundred thirty-four
Number::ordinal()
#
Convert the given number to ordinal form.
Number::ordinal(int|float $number, ?string $locale): string
Usage#
echo Number::ordinal(42); // 42nd
Number::percentage()
#
Convert the given number to its percentage equivalent.
Number::percentage(int|float $number, int $precision, int|null $maxPrecision, ?string $locale): string|false
Usage#
echo Number::percentage(0.75); // 1%
Number::currency()
#
Convert the given number to its currency equivalent.
Number::currency(int|float $number, string $in, ?string $locale): string|false
Usage#
echo Number::currency(1234.56, 'EUR'); // €1,234.56
Number::fileSize()
#
Convert the given number to its file size equivalent.
Number::fileSize(int|float $bytes, int $precision, int|null $maxPrecision): string
Usage#
echo Number::fileSize(1024); // 1 KB
Number::abbreviate()
#
Convert the number to its human-readable equivalent with abbreviated units.
Number::abbreviate(int|float $number, int $precision, int|null $maxPrecision): string
Number::forHumans()
#
Convert the number to its human-readable equivalent.
Number::forHumans(int|float $number, int $precision, ?int $maxPrecision, bool $abbreviate): string
Usage#
echo Number::forHumans(1234567.89); // 1 million
Number::useLocale()
#
Set the default locale.
Number::useLocale(string $locale): void
License#
This package is open-sourced software licensed under the MIT License.
Attributions#
The Number utility is ported from Laravel, licensed under the MIT Licence.
Contributing#
Contributions are welcome! Please see CONTRIBUTING.md for details.