From 3c89e9cd83d7adf79a7708782e8f96a01417a464 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Thu, 16 Nov 2023 13:38:18 -0600 Subject: [PATCH 1/3] Add docs for number utility functions --- helpers.md | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/helpers.md b/helpers.md index 907476208d0..f6c79d3cc57 100644 --- a/helpers.md +++ b/helpers.md @@ -83,6 +83,20 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct [last](#method-last) + +### Numbers + +
+ +[Number::format](#method-number-format) +[Number::toPercentage](#method-number-to-percentage) +[Number::toCurrency](#method-number-to-currency) +[Number::toFileSize](#method-number-to-file-size) +[Number::forHumans](#method-number-for-humans) + +
+ + ### Paths @@ -1080,6 +1094,105 @@ The `last` function returns the last element in the given array: // 300 + +## Numbers + +> **Warning** +> Several of the `Number` methods require the PHP `intl` extension. + + +#### `Number::format()` {.collection-method} + +The `Number::format` method determines the formatting of a value by grouped thousands: + + use Illuminate\Support\Number; + + $number = Number::format(100000); + + // 100,000 + + $number = Number::format(100000, 'de'); + + // 100.000 + + +#### `Number::toPercentage()` {.collection-method} + +The `Number::toPercentage` method returns the percentage representation of the given value: + + use Illuminate\Support\Number; + + $percentage = Number::toPercentage(10); + + // 10% + + $percentage = Number::toPercentage(10, 2); + + // 10.00% + +You may pass a third argument to the method to specify the percentage locale: + + $percentage = Number::toPercentage(10, 2, 'de'); + + // 10,00% + + +#### `Number::toCurrency()` {.collection-method} + +The `Number::toCurrency` method returns the currency representation of the provided numerical value: + + use Illuminate\Support\Number; + + $currency = Number::toCurrency(1000); + + // $1,000 + + $currency = Number::toCurrency(1000, 'EUR'); + + // €1,000 + +You may pass a third argument to the method to specify the currency locale: + + $currency = Number::toCurrency(1000, 'EUR', 'de'); + + // 1.000 € + + +#### `Number::toFileSize()` {.collection-method} + +The `Number::toFileSize` method returns the file size representation of the given numerical value in bytes: + + use Illuminate\Support\Number; + + $size = Number::toFileSize(1024); + + // 1 KB + + $size = Number::toFileSize(1024, 2); + + // 1.00 KB + + +#### `Number::forHumans()` {.collection-method} + +The `Number::forHumans()` method returns the human-readable format of a provided numberical value: + + use Illuminate\Support\Number; + + $number = Number::forHumans(1000); + + // 1 thousand + + $number = Number::forHumans(489939); + + // 490 thousand + +You may pass a second argument to the method to specify the number of decimal places: + + $number = Number::forHumans(1000, 2); + + // 1.00 thousand + ## Paths From efd36e6a01312059555584694e115d5aa6bd5edc Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Thu, 16 Nov 2023 13:42:44 -0600 Subject: [PATCH 2/3] Typo --- helpers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.md b/helpers.md index f6c79d3cc57..1bb3bb45116 100644 --- a/helpers.md +++ b/helpers.md @@ -1172,7 +1172,7 @@ The `Number::toFileSize` method returns the file size representation of the give // 1.00 KB - + #### `Number::forHumans()` {.collection-method} The `Number::forHumans()` method returns the human-readable format of a provided numberical value: From 4bb64c18127e9b2c28e2626324eb054047defdd3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 21 Nov 2023 10:54:15 -0600 Subject: [PATCH 3/3] formatting --- helpers.md | 73 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/helpers.md b/helpers.md index 1bb3bb45116..be5574d6b54 100644 --- a/helpers.md +++ b/helpers.md @@ -89,9 +89,9 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
[Number::format](#method-number-format) -[Number::toPercentage](#method-number-to-percentage) -[Number::toCurrency](#method-number-to-currency) -[Number::toFileSize](#method-number-to-file-size) +[Number::percentage](#method-number-percentage) +[Number::currency](#method-number-currency) +[Number::fileSize](#method-number-file-size) [Number::forHumans](#method-number-for-humans)
@@ -1097,13 +1097,10 @@ The `last` function returns the last element in the given array: ## Numbers -> **Warning** -> Several of the `Number` methods require the PHP `intl` extension. - #### `Number::format()` {.collection-method} -The `Number::format` method determines the formatting of a value by grouped thousands: +The `Number::format` method formats the given number into a locale specific string: use Illuminate\Support\Number; @@ -1111,71 +1108,83 @@ The `Number::format` method determines the formatting of a value by grouped thou // 100,000 - $number = Number::format(100000, 'de'); + $number = Number::format(100000, precision: 2); + + // 100,000.00 + + $number = Number::format(100000.123, maxPrecision: 2); + + // 100,000.12 + + $number = Number::format(100000, locale: 'de'); // 100.000 - -#### `Number::toPercentage()` {.collection-method} + +#### `Number::percentage()` {.collection-method} -The `Number::toPercentage` method returns the percentage representation of the given value: +The `Number::percentage` method returns the percentage representation of the given value as a string: use Illuminate\Support\Number; - $percentage = Number::toPercentage(10); + $percentage = Number::percentage(10); // 10% - $percentage = Number::toPercentage(10, 2); + $percentage = Number::percentage(10, precision: 2); // 10.00% -You may pass a third argument to the method to specify the percentage locale: + $percentage = Number::percentage(10.123, maxPrecision: 2); - $percentage = Number::toPercentage(10, 2, 'de'); + // 10.12% + + $percentage = Number::percentage(10, precision: 2, locale: 'de'); // 10,00% - -#### `Number::toCurrency()` {.collection-method} + +#### `Number::currency()` {.collection-method} -The `Number::toCurrency` method returns the currency representation of the provided numerical value: +The `Number::currency` method returns the currency representation of the given value as a string: use Illuminate\Support\Number; - $currency = Number::toCurrency(1000); + $currency = Number::currency(1000); // $1,000 - $currency = Number::toCurrency(1000, 'EUR'); + $currency = Number::currency(1000, in: 'EUR'); // €1,000 -You may pass a third argument to the method to specify the currency locale: - - $currency = Number::toCurrency(1000, 'EUR', 'de'); + $currency = Number::currency(1000, in: 'EUR', locale: 'de'); // 1.000 € - -#### `Number::toFileSize()` {.collection-method} + +#### `Number::fileSize()` {.collection-method} -The `Number::toFileSize` method returns the file size representation of the given numerical value in bytes: +The `Number::fileSize` method returns the file size representation of the given byte value as a string: use Illuminate\Support\Number; - $size = Number::toFileSize(1024); + $size = Number::fileSize(1024); // 1 KB - $size = Number::toFileSize(1024, 2); + $size = Number::fileSize(1024 * 1024); + + // 1 MB + + $size = Number::fileSize(1024, precision: 2); // 1.00 KB #### `Number::forHumans()` {.collection-method} -The `Number::forHumans()` method returns the human-readable format of a provided numberical value: +The `Number::forHumans()` method returns the human-readable format of the provided numerical value: use Illuminate\Support\Number; @@ -1187,11 +1196,9 @@ The `Number::forHumans()` method returns the human-readable format of a provided // 490 thousand -You may pass a second argument to the method to specify the number of decimal places: - - $number = Number::forHumans(1000, 2); + $number = Number::forHumans(1230000, precision: 2); - // 1.00 thousand + // 1.23 million ## Paths