Skip to content

Commit d509f3d

Browse files
authored
Update Number::toReadableSize() to new specifications
1 parent 340a585 commit d509f3d

1 file changed

Lines changed: 41 additions & 14 deletions

File tree

docs/en/core-libraries/number.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,55 @@ 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+
- 1000 Bytes = 1 KB
205+
- 1024 Bytes = 1 KiB
206+
207+
## Setting the Default Byte Units
208+
209+
### Number::setUseIecUnits()
210+
211+
`static` Cake\\I18n\\Number::**setUseIecUnits**(bool $useIec): void
212+
213+
This method acts as a setter for the default byte units. It eliminates the
214+
need to pass the boolean parameter to `Cake\I18n\Number::toReadableSize()` when
215+
switching between decimal units and IEC units. If `$useIec` is defined as true,
216+
IEC units will be employed; otherwise, decimal units will be used.
217+
191218
## Formatting Numbers
192219

193220
### Number::format()

0 commit comments

Comments
 (0)