Get your next remote job on LaraJobs.

The ultimate guide to Laravel's new Number helper

The ultimate guide to Laravel's new Number helper

Introduction to the Laravel Number helper class

Working with numbers in your web applications just got a whole lot easier! 🔥

The Laravel number helper is a new utility class designed to make number-related tasks a breeze. Think formatting, currencies, and even converting bytes into human-readable file sizes.

This handy tool comes packed with methods that cater to different aspects of number manipulation, so let’s dive into some of the key methods that you can use right away.

Set Laravel’s Number helper locale

When dealing with international applications, setting the correct locale is crucial for number formatting. The useLocale method allows you to set the default locale which will influence how numbers are presented.

To set it up, add the following code in the boot() method of your AppServiceProvider class:

use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
public function boot()

Need to execute a function with a specific locale temporarily? No worries! Use the withLocale method to take care of that. Here’s how to proceed:

use Illuminate\Support\Number;
// Temporarily use German locale for a closure execution.
$germanNumber = Number::withLocale('de', fn () => Number::format(1234567.89));
echo $germanNumber; // "1.234.567,89"

In the above snippet, you see how smoothly “laravel number helper” can adapt to different locales, making your numbers relatable to a global audience.

Format numbers

The format method is your go-to for getting numbers to look their best. It takes care of things like decimal points and digit grouping according to your specified locale preferences. Check this out:

$formattedNumber = Number::format(1234.56, 2);
echo $formattedNumber; // "1,234.56"

This example showcases the “laravel number helper” with a precision of two decimal places. Simple and straight to the point!

Convert numbers to words

Ever needed to spell out a number in full text? The spell method of the “laravel number helper” transforms numbers into words, perfect for checks or formal documents.

$numberInWords = Number::spell(102);
echo $numberInWords; // "one hundred and two"

Generate ordinal numbers (“1st”, “2nd”, “3rd”, etc.)

The ordinal method helps express numbers in their ordinal form. Whether it’s 1st, 2nd, 3rd, or beyond, see what it looks like here:

$ordinalNumber = Number::ordinal(21);
echo $ordinalNumber; // "21st"

Laravel’s Number helper gracefully handles this conversion, so you don’t have to.

Format percentages

Percentages are everywhere – discounts, statistics, you name it. With Laravel’s number helper, the percentage method renders percentages neatly.

$percentage = Number::percentage(75.125, 1);
echo $percentage; // "75.1%"

The example shows how the method factors in precision for your percentage values.

Format currencies

Currency formatting is crucial for any financial application. The currency method in “laravel number helper” ensures your monetary values are readable and professional.

$currencyValue = Number::currency(5000, 'EUR');
echo $currencyValue; // "€5,000.00"

Global currencies are no hurdle for Laravel’s Number helper.

Determine file sizes

Understanding file sizes in bytes can be baffling. The fileSize method converts bytes into a file size that makes sense to anyone. Let’s say you have 2048 bytes:

$fileSize = Number::fileSize(2048);
echo $fileSize; // "2 KB"

Laravel’s Number helper simplifies file size presentation.

Abbreviate large numbers (Laravel 10.35)

The abbreviate method is perfect for large numbers that need simplifying. It’s useful for social media stats or any place where space is at a premium.

$abbreviatedNumber = Number::abbreviate(2500000);
echo $abbreviatedNumber; // "2.5M"

This is the Laravel’s Number helper at its most concise.

Show large numbers in a human-friendly format

The forHumans method expands on abbreviating by offering you the choice to represent large numbers in full words for even greater clarity.

$humanReadableNumber = Number::forHumans(123456789);
echo $humanReadableNumber; // "123 million"

And with that, the Laravel’s Number helper makes large numbers a lot less intimidating.


Blazing through number formatting challenges just got easier with Laravel’s Number helper.

Each method is designed to handle different nuances associated with number representation, from locales to human-readable formats

It’s time to explore these methods and put them to work in your Laravel applications! 💪

Be the first to comment!

Get help or share something of value with other readers!

Great deals for enterprise developers
The latest community links

Recommended articles

20+ Laravel best practices, tips and tricks to use in 2024

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

A complete history of Laravel's versions (2011-2024)

What's the current version of Laravel? Is there any LTS release you can rely on? And what about the history of the framework? Let's find out!

Laravel interview questions and answers for 2024

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

How does Laravel work? A crystal clear explanation.

Discover my step by step and simple explanation of how Laravel makes your life easier.

Understanding Laravel's fake() helper

Laravel's fake() helper is a powerful tool for generating fake data. Learn how to use it for seeding databases and prototyping.

7 Laravel RESTful APIs best practices for 2024

Master the art of crafting RESTful APIs with Laravel thanks to these best practices.

Laravel 10: the upgrade guide from version 9

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

- / -