Laravel

Laravel Introducing a New Utility Class: Number

Hello, Laravel Friends.

Welcome to the MageComp Laravel blog.

Laravel is a rapidly evolving framework, and new features and utilities are being introduced by the platform frequently. Today, in this Laravel blog, we will learn about Numbers that Laravel introduces as a new utility class.

What is a Utility Class in Laravel?

In Laravel, a utility class is not a specific term used in the framework itself. However, the term “utility class” is often used in programming in a broader sense to refer to a class that provides general utility functions or methods that can be used across different parts of an application. In Laravel, you may find such utility-like classes or helper functions that offer common functionalities.

The Laravel Number Class

Introduction

All methods are part of the Illuminate\Support\Number class:

use Illuminate\Support\Number;

General number formatting

Using the format method, we can format a number according to the current locale.

Number::format(25) // 25
Number::format(100000) // 100,000
Number::format(123456789) // 123,456,789

We can also specify a custom locale to format the number according to that locale’s rules.

Number::format(123456789, locale: 'en') // 123,456,789
Number::format(123456789, locale: 'de') // 123.456.789
Number::format(123456789, locale: 'sv') // 123 456 789

Percentage formatting

The formatPercentage method formats a number as a percentage according to the current locale.

Number::percentage(25) // 25%
Number::percentage((1/3) * 100, precision: 2) // 33.33%

Currency formatting

Here’s another fun method used to format various currencies with locale support. Perfect for your webshops!

Number::toCurrency(10) // $10.00
Number::toCurrency(25, currency: 'EUR') // €25.00
Number::toCurrency(5.49, currency: 'EUR', locale: 'de') // 5.49 €

File size formatting

Here is the toFileSize method which actually is the whole reason behind this utility class. I first submitted a PR to add a File::bytesToHuman() helper (PR #48827), which Taylor then suggested we add as part of a new Number class.

Number::toFileSize(1024); // 1 KB
Number::toFileSize(1600, precision: 2); // 1.56 KB
Number::toFileSize(1024 * 1024 * 1024 * 5); // 5 GB

Human readable formatting

Next up is also quite a fun one for when you want something that’s more readable than precise. It converts numbers to a human-readable string.

Number::forHumans(1000) // 1 thousand
Number::forHumans(12345) // 12 thousand
Number::forHumans(12345, precision: 3) // 12.345 thousand

Set the locale

We can set the locale globally using the setLocale method, for example, in a service provider:

Number::setLocale('sv');

You can also use the withLocale method, which executes the given callback using the specified locale and then restores the original locale:

Number::withLocale('sv', function () {
    return Number::format(123456789);
});

Conclusion

Hope you found this new number formatting addition in Laravel helpful. It’s always great to have tools that simplify our work, and we encourage you to explore the upcoming Laravel release and consider integrating this utility class into your projects.  It can save you time and effort in formatting numbers, making your work more efficient and effective.

Happy coding!!!

Click to rate this post!
[Total: 1 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago