{"id":4746,"date":"2023-12-02T04:02:07","date_gmt":"2023-12-02T04:02:07","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=4746"},"modified":"2024-01-14T05:32:24","modified_gmt":"2024-01-14T05:32:24","slug":"laravel-number-utility-class","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/laravel-number-utility-class\/","title":{"rendered":"Laravel Number Utility Class"},"content":{"rendered":"<p>Hello everyone, Caen here! I&#8217;m excited to share with you a recent addition I made to the Laravel framework (<a href=\"https:\/\/github.com\/laravel\/framework\/pull\/48845\">PR #48845<\/a>) that I believe will simplify your number formatting needs. I&#8217;ve introduced a new\u00a0<code>Number<\/code>\u00a0utility class that provides several new helpers to format numbers, and it should be available in the next Laravel release this week.<\/p>\n<p><strong>As another bonus<\/strong>: the helpers are locale-aware, so you can format numbers according to the current locale, either globally, or on a per-method basis!<\/p>\n<h2>Background<\/h2>\n<p>In many applications, there&#8217;s often a need to format numbers according to different requirements, such as displaying them as currency, percentages, or human-readable file sizes. Laravel didn&#8217;t have a dedicated utility for this, so with the help of the community, I decided to create one. I&#8217;ve been working on this utility class for a a little while now, and am really excited that it was merged into the framework. Let&#8217;s take a look at what it offers.<\/p>\n<p><strong>The Laravel\u00a0<code>Number<\/code>\u00a0Class<\/strong><\/p>\n<h3>Introduction<\/h3>\n<p>All methods are part of the\u00a0<code>Illuminate\\Support\\Number<\/code>\u00a0class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">use Illuminate\\Support\\Number;<\/pre>\n<h3>General number formatting<\/h3>\n<p>Using the\u00a0<code>format<\/code>\u00a0method, we can format a number according to the current locale.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::format(25) \/\/ 25\r\nNumber::format(100000) \/\/ 100,000\r\nNumber::format(123456789) \/\/ 123,456,789<\/pre>\n<p>We can also specify a custom locale to format the number according to that locale&#8217;s rules.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::format(123456789, locale: 'en') \/\/ 123,456,789\r\nNumber::format(123456789, locale: 'de') \/\/ 123.456.789\r\nNumber::format(123456789, locale: 'sv') \/\/ 123 456 789<\/pre>\n<h3>Percentage formatting<\/h3>\n<p>The\u00a0<code>formatPercentage<\/code>\u00a0method formats a number as a percentage according to the current locale.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::percentage(25) \/\/ 25%\r\nNumber::percentage((1\/3) * 100, precision: 2) \/\/ 33.33%<\/pre>\n<h3>Currency formatting<\/h3>\n<p>Here&#8217;s another fun method used to format various currencies with locale support. Perfect for your webshops!<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::toCurrency(10) \/\/ $10.00\r\nNumber::toCurrency(25, currency: 'EUR') \/\/ \u20ac25.00\r\nNumber::toCurrency(5.49, currency: 'EUR', locale: 'de') \/\/ 5.49 \u20ac<\/pre>\n<h3>File size formatting<\/h3>\n<p>Here is the\u00a0<code>toFileSize<\/code>\u00a0method which actually is the whole reason behind this utility class. I first submitted a PR to add a\u00a0<code>File::bytesToHuman()<\/code>\u00a0helper (<a href=\"https:\/\/github.com\/laravel\/framework\/pull\/48827\">PR #48827<\/a>), which\u00a0<a href=\"https:\/\/github.com\/laravel\/framework\/pull\/48827#issuecomment-1781232805\">Taylor then suggested<\/a>\u00a0we add as part of a new\u00a0<code>Number<\/code>\u00a0class.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::toFileSize(1024); \/\/ 1 KB\r\nNumber::toFileSize(1600, precision: 2); \/\/ 1.56 KB\r\nNumber::toFileSize(1024 * 1024 * 1024 * 5); \/\/ 5 GB<\/pre>\n<h3>Human readable formatting<\/h3>\n<p>Next up is also a quite fun one for when you want something that&#8217;s more readable than precise. It converts numbers to a human-readable string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::forHumans(1000) \/\/ 1 thousand\r\nNumber::forHumans(12345) \/\/ 12 thousand\r\nNumber::forHumans(12345, precision: 3) \/\/ 12.345 thousand<\/pre>\n<h3>Setting the locale<\/h3>\n<p>We can set the locale globally using the\u00a0<code>setLocale<\/code>\u00a0method, for example in a service provider:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::setLocale('sv');<\/pre>\n<p>You can also use the\u00a0<code>withLocale<\/code>\u00a0method which executes the given callback using the specified locale and then restores the original locale:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">Number::withLocale('sv', function () {\r\n    return Number::format(123456789);\r\n});<\/pre>\n<h2>Conclusion<\/h2>\n<p>I hope this new addition makes your life a bit easier when dealing with number formatting in Laravel. Please free to check out the next Laravel release and incorporate this utility class into your projects. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone, Caen here! I&#8217;m excited to share with you a recent <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/laravel-number-utility-class\/\" title=\"Laravel Number Utility Class\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":4747,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[708,722,676],"newstopic":[597,574],"class_list":{"0":"post-4746","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-web-development","8":"tag-laravel","9":"tag-laravel-number-utility-class","10":"tag-rengga-dev","11":"newstopic-laravel","12":"newstopic-php"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=4746"}],"version-history":[{"count":4,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4746\/revisions"}],"predecessor-version":[{"id":4751,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/4746\/revisions\/4751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/4747"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=4746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=4746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=4746"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=4746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}