Skip to content

Instantly share code, notes, and snippets.

@thomseddon
Last active March 8, 2023 03:39
Show Gist options
  • Save thomseddon/3511330 to your computer and use it in GitHub Desktop.
Save thomseddon/3511330 to your computer and use it in GitHub Desktop.
AngularJS byte format filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
@nikhilkaluskar
Copy link

Very useful. Thanks

@cognivator
Copy link

Nice.

@sydcanem
Copy link

Timesaver. Thanks.

@icem
Copy link

icem commented Jun 2, 2015

Just use a8m/angular-filter kbFmt filter

@basvdijk
Copy link

basvdijk commented Jul 9, 2015

Thanks!

@beannguyen
Copy link

Thank you!
It`s helpful.

@alehano
Copy link

alehano commented Oct 4, 2015

Cool. But I would add
if (bytes === 0) return '0 B';
on 4th line.

@ziomio
Copy link

ziomio commented Dec 22, 2015

Just use a8m/angular-filter kbFmt filter
@icem, thanks for the tip!

@sjparsons
Copy link

Nice work, cheers! And thanks esp for the tests @veewee

@johnknoop
Copy link

If you want to make use of Angulars i18n functionality, then you can reuse the Number filter to format the number. In my case, I want the comma as decimal separator.

Here's the code:

var numberFilter = filter("number");
return (bytes, precision) => {
    if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
    if (typeof precision === 'undefined') precision = 1;
    var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
        number = Math.floor(Math.log(bytes) / Math.log(1024));
    return numberFilter(bytes / Math.pow(1024, Math.floor(number)), precision) + ' ' + units[number];
}

@yjchen-tw
Copy link

Thank you~

@selamialtin
Copy link

nice, Thanks.

@fuhaixiao
Copy link

谢谢

@pouyanh
Copy link

pouyanh commented May 14, 2016

tnx

@pbek
Copy link

pbek commented Aug 5, 2016

thank you!

@nextsummer33
Copy link

When bytes = 0, it showed NaN undefined.

Handle when bytes = 0 as following
if (bytes === 0 || isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';

@samreaves
Copy link

Very helpful. Thank you!

@tamas-marton
Copy link

tamas-marton commented Nov 14, 2016

@rapilabs has a point (though I guess the correct symbol is KiB, not KB - https://en.wikipedia.org/wiki/Kibibyte), it seems to be better to use 1000 as a multiplier instead of 1024 (that way, the user gets the same number they see in the OS file browser). Anyway, great code, thank you very much.

@superole
Copy link

it seems to be better to use 1000 as a multiplier instead of 1024 (that way, the user gets the same number they see in the OS file browser)

I don't know which OS you are referring to, @tamas-marton, but this is certainly not true in Windows. I just verified that both win7 and win10 uses 1024 as base for their units ;-)

@git-demyan
Copy link

Thanks !!!

@Xsmael
Copy link

Xsmael commented Jul 5, 2017

if you wanna handle zero values as well and display them, change the 6th line like so:
number = Math.floor(Math.log(bytes) / Math.log(1024)) | 0;

@rubenqba
Copy link

Awesome!! +1

@CLOUGH
Copy link

CLOUGH commented Jul 28, 2017

You just saved me 15 mins of my life

@ruiasn
Copy link

ruiasn commented Nov 30, 2017

Very nice! Thanks !!!

@dalco
Copy link

dalco commented Nov 30, 2017

Awesome! Thanks!

@dstran
Copy link

dstran commented Apr 6, 2018

Thank you!!

@piitr64
Copy link

piitr64 commented Apr 9, 2018

Perfect. Thanks!!!
if you wanna change dot to comma as decimal separator, just simply add:
.replace('.', ',')
Here is the complete 7th line:
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision).replace('.', ',') + ' ' + units[number];

@reznov11
Copy link

Thank you so much , very helpful 👍

@saurabhrathod35
Copy link

what if bytes size is 0.025

@devonc0
Copy link

devonc0 commented May 22, 2019

Here it is for Angular ( I just formatted this up a bit, am using Angular7) - I did not port in the Unit Tests, but didn't modify the values, so, might be able to Angularize those as well

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'byteFormatter'
})
export class ByteFormatterPipe implements PipeTransform {

  transform(bytes: string | number, precision: number): any {

    if (isNaN(parseFloat(bytes.toString())) || !isFinite(Number(bytes))) {
      return '-';
    }

    if (typeof precision === 'undefined') {
      precision = 1;
    }

    const units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];
    const actualValue = Math.floor(Math.log(Number(bytes)) / Math.log(1024));

    return (Number(bytes) / Math.pow(1024, Math.floor(actualValue))).toFixed(precision) + ' ' + units[actualValue];

  }
}

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