---
title: "Sizes"
description: "Read a size written by a person (2M, 1.5Mi) in bytes with ByteSize"
type: "docs"
category: "doc"
tags: []
authors: [Anonymous]
date: "2026-11-05"
last_update: "2026-11-05"
time_minutes: 1
draft: false
unlisted: false
url: "https://www.derafu.dev/docs/core/file/sizes"
---

# Sizes

`ByteSize::parse($size)` reads a size, as written by a person, and returns it in bytes, or `null` if it is not a valid size.

```php
use Derafu\File\ByteSize;

ByteSize::parse('2048');     // 2048
ByteSize::parse('2M');       // 2000000
ByteSize::parse('1.5Mi');    // 1572864
ByteSize::parse('2MB');      // null
```

It is a whole number of bytes, or a number with a unit that can have decimals with a point. The units are not case sensitive:

| Unit | Bytes |
| --- | --- |
| `k`, `m`, `g` | Decimal: 1000, 1000<sup>2</sup>, 1000<sup>3</sup>. |
| `ki`, `mi`, `gi` | Binary: 1024, 1024<sup>2</sup>, 1024<sup>3</sup>. |

The result is rounded down to a whole byte (`1.1ki` is `1126`). The calculation is done with integers, so the rounding does not depend on how a float is stored.

Nothing else is a size: a unit alone (`M`), a number with spaces, a negative number or one with a sign, a decimal without a unit (`1.5`), a decimal point without decimals (`1.M`), a comma as the separator, an exponent, a unit that is not in the list (`MB`, `B`), a number with more than 9 decimals (without the zeros at the end), or a size that does not fit in an integer.



---
Last updated on 05/11/2026

