Good day everybody, Caen right here! I am excited to share with you a current addition I made to the Laravel framework (PR #48845) that I imagine will simplify your quantity formatting wants. I’ve launched a brand new Quantity
utility class that gives a number of new helpers to format numbers, and it needs to be obtainable within the subsequent Laravel launch this week.
As one other bonus: the helpers are locale-aware, so you’ll be able to format numbers in line with the present locale, both globally, or on a per-method foundation!
Background
In lots of purposes, there’s usually a must format numbers in line with completely different necessities, resembling displaying them as forex, percentages, or human-readable file sizes. Laravel did not have a devoted utility for this, so with the assistance of the group, I made a decision to create one. I have been engaged on this utility class for a a short while now, and am actually excited that it was merged into the framework. Let’s check out what it affords.
The Laravel Quantity
Class
Introduction
All strategies are a part of the IlluminateSupportNumber
class:
use IlluminateSupportNumber;
Normal quantity formatting
Utilizing the format
methodology, we are able to format a quantity in line with the present locale.
Quantity::format(25) // 25
Quantity::format(100000) // 100,000
Quantity::format(123456789) // 123,456,789
We will additionally specify a customized locale to format the quantity in line with that locale’s guidelines.
Quantity::format(123456789, 'en') // 123,456,789
Quantity::format(123456789, 'de') // 123.456.789
Quantity::format(123456789, 'sv') // 123 456 789
Share formatting
The formatPercentage
methodology codecs a quantity as a share in line with the present locale.
Quantity::toPercentage(25) // 25%
Quantity::toPercentage((1/3) * 100, precision: 2) // 33.33%
Foreign money formatting
Here is one other enjoyable methodology used to format varied currencies with locale assist. Good in your webshops!
Quantity::toCurrency(10) // $10.00
Quantity::toCurrency(25, forex: 'EUR') // €25.00
Quantity::toCurrency(5.49, forex: 'EUR', locale: 'de') // 5.49 €
File dimension formatting
Right here is the toFileSize
methodology which truly is the entire purpose behind this utility class. I first submitted a PR so as to add a File::bytesToHuman()
helper (PR #48827), which Taylor then advised we add as a part of a brand new Quantity
class.
Quantity::toFileSize(1024); // 1 KB
Quantity::toFileSize(1600, precision: 2); // 1.56 KB
Quantity::toFileSize(1024 * 1024 * 1024 * 5); // 5 GB
Human readable formatting
Subsequent up can also be a fairly enjoyable one for while you need one thing that is extra readable than exact. It converts numbers to a human-readable string.
Quantity::forHumans(1000) // 1 thousand
Quantity::forHumans(12345) // 12 thousand
Quantity::forHumans(12345, precision: 3) // 12.345 thousand
Setting the locale
We will set the locale globally utilizing the setLocale
methodology, for instance in a service supplier:
Quantity::setLocale('sv');
You can too use the withLocale
methodology which executes the given callback utilizing the required locale after which restores the unique locale:
Quantity::withLocale('sv', perform () {
return Quantity::format(123456789);
});
Conclusion
I hope this new addition makes your life a bit simpler when coping with quantity formatting in Laravel. Please free to take a look at the subsequent Laravel launch and incorporate this utility class into your tasks. Comfortable coding!
Replace 11/21/2023: The Numbers helper is now documented within the official Laravel helpers documentation.