@@ -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