Laravel Magazine
The Number Helper: Formatting Currency, Percentages, and File Sizes Without a Package

The Number Helper: Formatting Currency, Percentages, and File Sizes Without a Package

Eric Van Johnson ·

If you have ever reached for number_format(), written a tiny helper function to turn bytes into "4.2 MB," or pulled in a whole package just to print a percentage, Laravel's Number class already does all of that for you. It has quietly become one of the most useful, least talked about helpers in the framework.

Currency

use Illuminate\Support\Number;

Number::currency(1500);
// $1,500.00

Number::currency(1500, in: 'EUR');
// €1,500.00

Number::currency(49.9, in: 'USD', precision: 0);
// $50

Number::currency() wraps PHP's NumberFormatter under the hood, so it respects locale rules for you instead of making you hand-roll separators and symbols.

Percentages

Number::percentage(72.5);
// 72.50%

Number::percentage(72.5, precision: 0);
// 73%

Number::percentage(0.5, precision: 1, maxPrecision: 3);
// 0.500%

This is handy for dashboards at PhpArchitect where we show conversion rates and uptime numbers pulled straight from a query without extra string manipulation in the Blade view.

File sizes

Number::fileSize(1024);
// 1 KB

Number::fileSize(1024 * 1024 * 3.5);
// 3.5 MB

Number::fileSize(500, precision: 2);
// 500.00 B

This one alone replaces a chunk of boilerplate most Laravel apps have copy-pasted from Stack Overflow for years.

Ordinal and spelled-out numbers

Number::ordinal(3);
// 3rd

Number::spell(42);
// forty-two

Number::spell(3, after: 10);
// 3 (numbers 10 and under are spelled out, above is left as-is by default config)

Locale-aware formatting for the whole app

You are not stuck calling in: on every call. Set a default locale once in a service provider and every Number call downstream picks it up:

use Illuminate\Support\Number;

Number::useLocale('de');

Number::currency(1500);
// 1.500,00 $

Why this matters

None of this is complicated to build yourself, which is exactly the point. Number is one of those helpers that only saves you fifteen minutes at a time, but you will use it in nearly every project: invoices, admin dashboards, API responses, CSV exports. Multiply fifteen minutes by every place you format a dollar amount or a percentage across a codebase, and it adds up to a meaningfully cleaner set of views and controllers.

Next time you are about to write number_format($amount, 2) followed by manually gluing on a currency symbol, reach for Number::currency() instead.

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.