Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-10T16:20:15.000Z
Answers.Text: 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
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-15T07:47:03.460Z
Answers.Text: I've discovered that you can even round the `|filesize` filter by adding a round parameter: `|filesize(1)`. Maybe there's even more?
Answers.UserDisplayName: outline4
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-12T13:51:55.497Z
Answers.LastActivityDate: 2018-12-12T13:51:55.497Z
Answers.UserDisplayName: Romain P.
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-07T10:15:52.857Z
Answers.LastActivityDate: 2024-04-18T09:25:58.273Z
Answers.LastEditDate: 2024-04-18T09:25:58.273Z
Answers.UserDisplayName: smonist
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-06T23:19:08.960Z
Answers.LastActivityDate: 2017-10-24T15:27:07.333Z
Answers.LastEditDate: 2017-10-24T15:27:07.333Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <assets><format>
Title: Is there a way to format the Asset Size variable to Mb, Kb, etc.?
ThreadText: # 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](https://twig.symfony.com/doc/2.x/filters/number_format.html) 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;
  
  ```
  
  
  ---
  
  