Skip to content

Instantly share code, notes, and snippets.

@yrezgui
Created May 26, 2013 18:22
Show Gist options
  • Save yrezgui/5653591 to your computer and use it in GitHub Desktop.
Save yrezgui/5653591 to your computer and use it in GitHub Desktop.
This is a custom filter to show a bytes filesize in better way.
// add the filter to your application module
angular.module('myApp', ['filters']);
/**
* Filesize Filter
* @Param length, default is 0
* @return string
*/
angular.module('filters', [])
.filter('Filesize', function () {
return function (size) {
if (isNaN(size))
size = 0;
if (size < 1024)
return size + ' Bytes';
size /= 1024;
if (size < 1024)
return size.toFixed(2) + ' Kb';
size /= 1024;
if (size < 1024)
return size.toFixed(2) + ' Mb';
size /= 1024;
if (size < 1024)
return size.toFixed(2) + ' Gb';
size /= 1024;
return size.toFixed(2) + ' Tb';
};
});
/**
* Usage
* var myFile = 5678;
*
* {{myText|filesize}}
*
* Output
* "5.54 Kb"
*
*/
@tobegit3hub
Copy link

Clean and useful. Thanks for your work 👍

@kofj
Copy link

kofj commented Apr 25, 2015

It is useful.

@icem
Copy link

icem commented Jun 2, 2015

Just use a8m/angular-filter kbFmt filter

@patrick99e99
Copy link

@icem kbFmt filter does not support anything greater than GB.

@thewarpaint
Copy link

thewarpaint commented Apr 3, 2017

This is still in the first Google search results page, so, please don't use a loop for this. Have a look at https://gist.github.com/thomseddon/3511330#gistcomment-1703533 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment