DEV Community

Cover image for Exploring the Enhanced Number Format Helper in Laravel 10
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

Exploring the Enhanced Number Format Helper in Laravel 10

Laravel, the PHP framework known for its elegant syntax and developer-friendly features, has recently rolled out version 10, introducing a host of new functionalities and improvements. One standout feature is the enhanced Number Format Helper, designed to simplify the formatting of numbers, percentages, currency, and file sizes, and provide human-readable representations.

1. Number Formatting with Number::format()

The Number::format() method in Laravel 10 allows developers to easily format numerical values into locale-specific strings. Here are some examples of its usage:

use Illuminate\Support\Number;

$number = Number::format(100000);
// Result: 100,000

$number = Number::format(100000, precision: 2);
// Result: 100,000.00

$number = Number::format(100000.123, maxPrecision: 2);
// Result: 100,000.12

$number = Number::format(100000, locale: 'de');
// Result: 100.000
Enter fullscreen mode Exit fullscreen mode

This versatile method supports precision, max precision, and locale options, allowing developers to tailor the formatting to their specific needs.

2. Calculating Percentages with Number::percentage()

The Number::percentage() method simplifies the task of obtaining the percentage representation of a given value. It offers precision and locale options for added flexibility:

use Illuminate\Support\Number;

$percentage = Number::percentage(10);
// Result: 10%

$percentage = Number::percentage(10, precision: 2);
// Result: 10.00%

$percentage = Number::percentage(10.123, maxPrecision: 2);
// Result: 10.12%

$percentage = Number::percentage(10, precision: 2, locale: 'de');
// Result: 10,00%
Enter fullscreen mode Exit fullscreen mode

This method streamlines the process of displaying percentages with customizable precision and locale settings.

3. Currency Representation with Number::currency()

Formatting monetary values is a common requirement in web development. Laravel 10's Number::currency() method simplifies this task with options for currency type and locale:

use Illuminate\Support\Number;

$currency = Number::currency(1000);
// Result: $1,000

$currency = Number::currency(1000, in: 'EUR');
// Result: €1,000

$currency = Number::currency(1000, in: 'EUR', locale: 'de');
// Result: 1.000 €
Enter fullscreen mode Exit fullscreen mode

Developers can now easily format monetary values according to their preferred currency and locale, streamlining the presentation of financial information.

4. File Size Representation with Number::fileSize()

Dealing with file sizes is a common requirement in applications. Laravel 10's Number::fileSize() method provides an intuitive way to represent file sizes:

use Illuminate\Support\Number;

$size = Number::fileSize(1024);
// Result: 1 KB

$size = Number::fileSize(1024 * 1024);
// Result: 1 MB

$size = Number::fileSize(1024, precision: 2);
// Result: 1.00 KB
Enter fullscreen mode Exit fullscreen mode

This method simplifies the task of presenting file sizes in a human-readable format with customizable precision.

5. Human-Readable Numbers with Number::forHumans()

The Number::forHumans() method in Laravel 10 provides a human-readable format for numerical values:

use Illuminate\Support\Number;

$number = Number::forHumans(1000);
// Result: 1 thousand

$number = Number::forHumans(489939);
// Result: 490 thousand

$number = Number::forHumans(1230000, precision: 2);
// Result: 1.23 million
Enter fullscreen mode Exit fullscreen mode

This method is particularly useful for making large numbers more understandable, and providing a clean and concise representation.

Conclusion

With the enhanced Number Format Helper in Laravel 10, developers now have a powerful set of tools at their disposal for formatting numbers, percentages, currency, file sizes, and creating human-readable representations. These new methods not only simplify common tasks but also contribute to the overall efficiency and clarity of Laravel 10 applications. As developers explore these features, they can expect a more streamlined experience when working with numerical data in their projects.

Top comments (0)