More conventional AngularJS filter to format file size

In previous post, I have included a snippet for formatting file size in either SI or IEC unit. However, the computer science industry does not seem to be buying suggestions from IEC. If you look around, IEC units (KiB, MiB…) are almost non-existent. So I have come out with this more conventional filter to format file size. In case you are not familiar with the convention, SI units are being used to mean 1024 instead of 1000 when it comes to file sizes (1kB = 1024B). Until the day the world changes to use IEC units, this filter is more useful than the previous one.

.module(‘myModule’, [])
.filter(‘formatByte’, function () {
‘use strict’;
    return function (size) {
var exp = Math.log(size) / Math.log(1024) | 0;
return (size / Math.pow(1024, exp)).toFixed(1) + ‘ ‘ +
((exp > 0) ? ‘KMGTPEZY’[exp — 1] + ‘B’ : ‘Bytes’);

If you need a file size formatter, this will be useful for you.

Update: For “kilobyte”, “KB” (uppercase) is used more often than “kB” (lowercase). That is the JEDEC convention which you can read here.

Like what you read? Give Wai Park Soon a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.