-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Update the documentation of Number::toReadableSize() to new specifications #8227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
j04chim
wants to merge
3
commits into
cakephp:5.next
Choose a base branch
from
j04chim:5.next
base: 5.next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,28 +166,56 @@ echo Number::toPercentage(0.45691, 1, [ | |
|
|
||
| ### Number::toReadableSize() | ||
|
|
||
| `method` Cake\\I18n\\Number::**toReadableSize**(string $size): string | ||
| `method` Cake\\I18n\\Number::**toReadableSize**(mixed $size, ?bool $useIecUnits = null): string | ||
|
|
||
| This method formats data sizes in human readable forms. It provides | ||
| a shortcut way to convert bytes to KB, MB, GB, and TB. The size is | ||
| displayed with a two-digit precision level, according to the size | ||
| of data supplied (i.e. higher sizes are expressed in larger | ||
| terms): | ||
| This method formats data sizes in human-readable forms. By default, it | ||
| converts bytes to KB, MB, GB, and TB. The parameter `$useIecUnits` and | ||
| the global setter `setUseIecUnits()` can be used to switch to ISO/IEC 80000-13 | ||
| units, which are KiB, MiB, GiB, and TiB. The size is displayed with a two-digit | ||
| precision level, according to the amount of data supplied (i.e., higher sizes | ||
| are expressed in larger terms): | ||
|
|
||
| ```php | ||
| // By default, decimal units are used | ||
| // Called as NumberHelper | ||
| echo $this->Number->toReadableSize(0); // 0 Byte | ||
| echo $this->Number->toReadableSize(1024); // 1 KB | ||
| echo $this->Number->toReadableSize(1321205.76); // 1.26 MB | ||
| echo $this->Number->toReadableSize(5368709120); // 5 GB | ||
| echo $this->Number->toReadableSize(0); // 0 Bytes | ||
| echo $this->Number->toReadableSize(1024); // 1.02 KB | ||
| echo $this->Number->toReadableSize(1321205.76); // 1.32 MB | ||
| echo $this->Number->toReadableSize(5368709120); // 5.37 GB | ||
|
|
||
| // Called as Number | ||
| echo Number::toReadableSize(0); // 0 Byte | ||
| echo Number::toReadableSize(1024); // 1 KB | ||
| echo Number::toReadableSize(1321205.76); // 1.26 MB | ||
| echo Number::toReadableSize(5368709120); // 5 GB | ||
| echo Number::toReadableSize(0); // 0 Bytes | ||
| echo Number::toReadableSize(1024); // 1.02 KB | ||
| echo Number::toReadableSize(1321205.76); // 1.32 MB | ||
| echo Number::toReadableSize(5368709120); // 5.37 GB | ||
|
|
||
| // Change default units to IEC units | ||
| $this->Number->setUseIecUnits(true); | ||
|
|
||
| // Bytes are now calculated with exponents of two using IEC units | ||
| echo $this->Number->toReadableSize(0); // 0 Bytes | ||
| echo $this->Number->toReadableSize(1024); // 1 KiB | ||
| echo $this->Number->toReadableSize(1321205.76); // 1.26 MiB | ||
| echo $this->Number->toReadableSize(5368709120); // 5 GiB | ||
| ``` | ||
|
|
||
| It should be noted that IEC units are exponents of two and decimal units of ten. | ||
| This mean that: | ||
|
|
||
| - 1000 Bytes = 1 KB | ||
| - 1024 Bytes = 1 KiB | ||
|
|
||
| ## Setting the Default Byte Units | ||
|
|
||
| ### Number::setUseIecUnits() | ||
|
|
||
| `static` Cake\\I18n\\Number::**setUseIecUnits**(bool $useIec): void | ||
|
|
||
| This method acts as a setter for the default byte units. It eliminates the | ||
| need to pass the boolean parameter to `Cake\I18n\Number::toReadableSize()` when | ||
| switching between decimal units and IEC units. If `$useIec` is defined as true, | ||
| IEC units will be employed; otherwise, decimal units will be used. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a little indication that notes when this feature was added? The docs are continues per major so this could be helpful. Something like |
||
| ## Formatting Numbers | ||
|
|
||
| ### Number::format() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we added the new way as BC flag. To opt in.