Skip to content

Commit e745a35

Browse files
authored
Merge pull request #8227 from j04chim/5.next
Update the documentation of Number::toReadableSize() to new specifications
2 parents f58f33d + bc1345a commit e745a35

2 files changed

Lines changed: 61 additions & 14 deletions

File tree

docs/en/appendices/5-4-migration-guide.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ bin/cake upgrade rector --rules cakephp54 <path/to/app/src>
2424

2525
## New Features
2626

27+
### I18n
28+
29+
- `Number::toReadableSize()` now calculates decimal units (KB, MB, GB and TB)
30+
using an exponent of ten, meaning that 1 KB is 1000 Bytes. The units from the
31+
previous calculation method, where 1024 Bytes equaled 1 KB, have been changed
32+
to KiB, MiB, GiB, and TiB as defined in ISO/IEC 80000-13. It is possible to
33+
switch between the two units using a new optional boolean parameter in
34+
`Number::toReadableSize()`, as well as the new global setter `Number::setUseIecUnits()`.
35+
2736
### Utility
2837

2938
- New `Cake\Utility\Fs\Finder` class provides a fluent, iterator-based API for

docs/en/core-libraries/number.md

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,66 @@ echo Number::toPercentage(0.45691, 1, [
166166

167167
### Number::toReadableSize()
168168

169-
`method` Cake\\I18n\\Number::**toReadableSize**(string $size): string
169+
`method` Cake\\I18n\\Number::**toReadableSize**(mixed $size, ?bool $useIecUnits = null): string
170170

171-
This method formats data sizes in human readable forms. It provides
172-
a shortcut way to convert bytes to KB, MB, GB, and TB. The size is
173-
displayed with a two-digit precision level, according to the size
174-
of data supplied (i.e. higher sizes are expressed in larger
175-
terms):
171+
This method formats data sizes in human-readable forms. By default, it
172+
converts bytes to KB, MB, GB, and TB. The parameter `$useIecUnits` and
173+
the global setter `setUseIecUnits()` can be used to switch to ISO/IEC 80000-13
174+
units, which are KiB, MiB, GiB, and TiB. The size is displayed with a two-digit
175+
precision level, according to the amount of data supplied (i.e., higher sizes
176+
are expressed in larger terms):
176177

177178
```php
179+
// By default, decimal units are used
178180
// Called as NumberHelper
179-
echo $this->Number->toReadableSize(0); // 0 Byte
180-
echo $this->Number->toReadableSize(1024); // 1 KB
181-
echo $this->Number->toReadableSize(1321205.76); // 1.26 MB
182-
echo $this->Number->toReadableSize(5368709120); // 5 GB
181+
echo $this->Number->toReadableSize(0); // 0 Bytes
182+
echo $this->Number->toReadableSize(1024); // 1.02 KB
183+
echo $this->Number->toReadableSize(1321205.76); // 1.32 MB
184+
echo $this->Number->toReadableSize(5368709120); // 5.37 GB
183185

184186
// Called as Number
185-
echo Number::toReadableSize(0); // 0 Byte
186-
echo Number::toReadableSize(1024); // 1 KB
187-
echo Number::toReadableSize(1321205.76); // 1.26 MB
188-
echo Number::toReadableSize(5368709120); // 5 GB
187+
echo Number::toReadableSize(0); // 0 Bytes
188+
echo Number::toReadableSize(1024); // 1.02 KB
189+
echo Number::toReadableSize(1321205.76); // 1.32 MB
190+
echo Number::toReadableSize(5368709120); // 5.37 GB
191+
192+
// Change default units to IEC units
193+
$this->Number->setUseIecUnits(true);
194+
195+
// Bytes are now calculated with exponents of two using IEC units
196+
echo $this->Number->toReadableSize(0); // 0 Bytes
197+
echo $this->Number->toReadableSize(1024); // 1 KiB
198+
echo $this->Number->toReadableSize(1321205.76); // 1.26 MiB
199+
echo $this->Number->toReadableSize(5368709120); // 5 GiB
189200
```
190201

202+
It should be noted that IEC units are exponents of two and decimal units of ten.
203+
This mean that:
204+
205+
- 1000 Bytes = 1 KB
206+
- 1024 Bytes = 1 KiB
207+
208+
::: info Modified in version 5.4.0
209+
It is now possible to use the byte units defined by the ISO/IEC 80000-13
210+
standard alongside more natural decimal units.
211+
:::
212+
213+
## Setting the Default Byte Units
214+
215+
### Number::setUseIecUnits()
216+
217+
`static` Cake\\I18n\\Number::**setUseIecUnits**(bool $useIec): void
218+
219+
This method acts as a setter for the default byte units. It eliminates the
220+
need to pass the boolean parameter to `Cake\I18n\Number::toReadableSize()` when
221+
switching between decimal units and IEC units. If `$useIec` is defined as true,
222+
IEC units will be employed; otherwise, decimal units will be used.
223+
224+
::: info Added in version 5.4.0
225+
This method has been added to remove the need to pass the optionnal boolean argument
226+
each time IEC units are needed.
227+
:::
228+
191229
## Formatting Numbers
192230

193231
### Number::format()

0 commit comments

Comments
 (0)