<p>You now can use the <code>filesize</code> filter:
<a href="https://docs.craftcms.com/v3/dev/filters.html#filesize" rel="noreferrer">https://docs.craftcms.com/v3/dev/filters.html#filesize</a>.
This will make the file size readable, and the unit will be selected automatically.</p>

CC BY-SA 4.0
2019-04-10T16:20:15.000Z
As an extension to this answer, as it is specific to the question, as of `Craft 3.1.21` the `filesize` filter now formats using metric units: https://github.com/craftcms/cms/blob/develop/CHANGELOG-v3.md#3121---2019-04-03
Jamie Wade
CC BY-SA 4.0
2019-10-15T07:47:03.460Z
I've discovered that you can even round the `|filesize` filter by adding a round parameter: `|filesize(1)`. Maybe there's even more?
outline4
CC BY-SA 4.0
2018-12-12T13:51:55.497Z
2018-12-12T13:51:55.497Z
Romain P.
<p>As mentioned before, the filesize filter is your friend: <a href="https://craftcms.com/docs/4.x/dev/filters.html#filesize" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/dev/filters.html#filesize</a></p>
<p>Internally, this uses a yii Formatter. You can supply additional arguments to the filter. Here is a link to the function Craft is calling internally: <a href="https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#asShortSize()-detail" rel="nofollow noreferrer">https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#asShortSize()-detail</a></p>
<p>Be aware that Craft's default base for calculating file sizes is 1000, not 1024. This leads to some unwanted results while I was working with it. I ended up overriding the global setting like this:</p>
<pre><code>Craft::$app-&gt;formatter-&gt;sizeFormatBase = 1024;
</code></pre>

CC BY-SA 4.0
2023-07-07T10:15:52.857Z
2024-04-18T09:25:58.273Z
2024-04-18T09:25:58.273Z
smonist
<p>This should do it:</p>

<pre><code>{% set bytes = asset.size %}
{% set kiloBytes = (bytes / 1024)|round(2) %}
{% set megaBytes = (kiloBytes / 1024)|round(2) %}

bytes = {{ bytes }}
kiloBytes = {{ kiloBytes }}
megaBytes = {{ megaBytes }}
</code></pre>

<p>You can look at Twig's <a href="https://twig.symfony.com/doc/2.x/filters/number_format.html" rel="noreferrer">number_format</a> filter if you want to get fancy with how to display the numbers.</p>

CC BY-SA 3.0
2015-01-06T23:19:08.960Z
2017-10-24T15:27:07.333Z
2017-10-24T15:27:07.333Z
Brad Bell
<p>I'm trying to display the file size of a file uploaded in Assets. I can get it to display the number of bytes in the file. <a href="https://craftcms.com/docs/templating/craft.assets#size" rel="noreferrer">https://craftcms.com/docs/templating/craft.assets#size</a></p>

<p>Is there a way to format that number to Mb's?</p>

<assets><format>
Is there a way to format the Asset Size variable to Mb, Kb, etc.?
# Is there a way to format the Asset Size variable to Mb, Kb, etc.?

(Asked by: username_0 on 2015-01-06)

I'm trying to display the file size of a file uploaded in Assets. I can get it to display the number of bytes in the file. <https://craftcms.com/docs/templating/craft.assets#size>

Is there a way to format that number to Mb's?

**Tags:** assets, format

---

## 3 answers

### Answer by username_5 on 2015-01-06. Score: 12 | Accepted answer

This should do it:
{% set bytes = asset.size %}
{% set kiloBytes = (bytes / 1024)|round(2) %}
{% set megaBytes = (kiloBytes / 1024)|round(2) %}

bytes = {{ bytes }}
kiloBytes = {{ kiloBytes }}
megaBytes = {{ megaBytes }}

You can look at Twig's number_format filter if you want to get fancy with how to display the numbers.


---

### Answer by username_1 on 2018-12-12. Score: 7

You now can use the `filesize` filter:
<https://docs.craftcms.com/v3/dev/filters.html#filesize>.
This will make the file size readable, and the unit will be selected automatically.

> **Comments:**
> **username_2**: As an extension to this answer, as it is specific to the question, as of `Craft 3.1.21` the `filesize` filter now formats using metric units: https://github.com/craftcms/cms/blob/develop/CHANGELOG-v3.md#3121---2019-04-03  
> **username_3**: I've discovered that you can even round the `|filesize` filter by adding a round parameter: `|filesize(1)`. Maybe there's even more?  

---

### Answer by username_4 on 2023-07-07. Score: 2

As mentioned before, the filesize filter is your friend: https://craftcms.com/docs/4.x/dev/filters.html#filesize
Internally, this uses a yii Formatter. You can supply additional arguments to the filter. Here is a link to the function Craft is calling internally: https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#asShortSize()-detail
Be aware that Craft's default base for calculating file sizes is 1000, not 1024. This leads to some unwanted results while I was working with it. I ended up overriding the global setting like this:
Craft::$app->formatter->sizeFormatBase = 1024;


---

