formatBytes(bytes: string | number, decimals?: number, asString?: boolean): string | {val: number, unit: string}
| Parameter | Type | Default | Description |
|---|---|---|---|
bytes |
string | number |
- | Byte count. |
decimals |
number |
2 |
Number of decimal places. |
asString |
boolean |
false |
If true, returns a formatted string; if false, returns an object with val and unit properties. |
string | {val: number, unit: string}22.4 MB
<div class="mb-3">
<label class="form-label">Bytes</label>
<input id="formatBytesInput" class="form-control form-control-solid" placeholder="Enter byte count...">
</div>
<div>
<span class="fw-bold">Result: </span>
<code id="formatBytesResult"></code>
</div>
import {utils} from 'metronic-extension';
const {formatBytes} = utils;
// As string
formatBytes(435, 2, true); // '435 Bytes'
formatBytes(490398, 2, true); // '478.9 KB'
formatBytes(23483023, 2, true); // '22.4 MB'
formatBytes(30498505889, 2, true); // '28.4 GB'
formatBytes(9485039485039445, 2, true); // '8.42 PB'
// As object (default)
formatBytes(9485039485039445); // {val: 8.42, unit: 'PB'}