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]; | |
} | |
}); |
This comment has been minimized.
This comment has been minimized.
Many thanks man- very helpful! |
This comment has been minimized.
This comment has been minimized.
Just what I needed! Thanks! |
This comment has been minimized.
This comment has been minimized.
nice! |
This comment has been minimized.
This comment has been minimized.
Thanks you! Very helpful!! |
This comment has been minimized.
This comment has been minimized.
Simple and very helpful. However, I had an issue when I pass in bytes=0, I was getting 'NaN Undefined'. Changed the line 3 to include a check and return '-' for this use case: Thanks |
This comment has been minimized.
This comment has been minimized.
Thank you very much, this was very helpful. |
This comment has been minimized.
This comment has been minimized.
Thanks, saved some time. :) |
This comment has been minimized.
This comment has been minimized.
Nice! Thanks |
This comment has been minimized.
This comment has been minimized.
Very nice! Thanks |
This comment has been minimized.
This comment has been minimized.
Nice! |
This comment has been minimized.
This comment has been minimized.
Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
If bytes is zero, it returns NaN undefined, so I added: if (bytes === 0) { return '0 bytes' } |
This comment has been minimized.
This comment has been minimized.
thanks! |
This comment has been minimized.
This comment has been minimized.
Perfect. Thanks. |
This comment has been minimized.
This comment has been minimized.
Nice one, cheers |
This comment has been minimized.
This comment has been minimized.
To be technically correct, a kilobyte (1024 bytes) is actually KB, not kB which is 1000 bytes |
This comment has been minimized.
This comment has been minimized.
Gooooooood |
This comment has been minimized.
This comment has been minimized.
Awesome, thanks! |
This comment has been minimized.
This comment has been minimized.
Helpful |
This comment has been minimized.
This comment has been minimized.
Great, thanks! |
This comment has been minimized.
This comment has been minimized.
It's simple and works just as advertised. And took a guy on his second day of using Angular a total of 10 seconds to set up. Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks! 'use strict';
describe('Filter: bytes', function () {
// load the filter's module
beforeEach(module('YOURMODULENAME'));
// initialize a new instance of the filter before each test
var bytes;
beforeEach(inject(function ($filter) {
bytes = $filter('bytes');
}));
it('should return nothing when there is no filesize', function () {
expect(bytes('text')).toBe('-');
});
it('should round the filesize based on the configured precision', function () {
var size = 1024 + 512;
expect(bytes(size)).toBe('1.5 kB');
expect(bytes(size, 2)).toBe('1.50 kB');
});
it('should recognize bytes', function () {
expect(bytes(1, 0)).toBe('1 bytes');
});
it('should recognize KiloBytes', function () {
expect(bytes(Math.pow(1024, 1), 0)).toBe('1 kB');
});
it('should recognize MegaBytes', function () {
expect(bytes(Math.pow(1024, 2), 0)).toBe('1 MB');
});
it('should recognize GigaBytes', function () {
expect(bytes(Math.pow(1024, 3), 0)).toBe('1 GB');
});
it('should recognize TeraBytes', function () {
expect(bytes(Math.pow(1024, 4), 0)).toBe('1 TB');
});
it('should recognize PetaBytes', function () {
expect(bytes(Math.pow(1024, 5), 0)).toBe('1 PB');
});
}); |
This comment has been minimized.
This comment has been minimized.
This is great. I forked a version with some more whitespace and explicit braces (https://gist.github.com/jessecurry/cd79fcc705cc3db97347).
|
This comment has been minimized.
This comment has been minimized.
Thanks for this filter. This can be further improved by removing trailing zeros from the result.
|
This comment has been minimized.
This comment has been minimized.
ありがとう! |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
I added this to a filter library here: https://github.com/niemyjski/angular-filters |
This comment has been minimized.
This comment has been minimized.
cool, thanks for sharing that! |
This comment has been minimized.
This comment has been minimized.
thanks ^^ |
This comment has been minimized.
This comment has been minimized.
Can I use this in a template directly? |
This comment has been minimized.
This comment has been minimized.
Cool! |
This comment has been minimized.
This comment has been minimized.
Quick search, found it, works, thanks :D. |
This comment has been minimized.
This comment has been minimized.
You miel thx |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
I just added some improvements to my version based off this to handle string numbers: https://github.com/exceptionless/angular-filters |
This comment has been minimized.
This comment has been minimized.
Thank you. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Very useful. Thanks |
This comment has been minimized.
This comment has been minimized.
Nice. |
This comment has been minimized.
This comment has been minimized.
Timesaver. Thanks. |
This comment has been minimized.
This comment has been minimized.
Just use a8m/angular-filter kbFmt filter |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
Cool. But I would add |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Nice work, cheers! And thanks esp for the tests @veewee |
This comment has been minimized.
This comment has been minimized.
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:
|
This comment has been minimized.
This comment has been minimized.
Thank you~ |
This comment has been minimized.
This comment has been minimized.
nice, Thanks. |
This comment has been minimized.
This comment has been minimized.
谢谢 |
This comment has been minimized.
This comment has been minimized.
tnx |
This comment has been minimized.
This comment has been minimized.
thank you! |
This comment has been minimized.
This comment has been minimized.
When bytes = 0, it showed NaN undefined. Handle when bytes = 0 as following |
This comment has been minimized.
This comment has been minimized.
Very helpful. Thank you! |
This comment has been minimized.
This comment has been minimized.
@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. |
This comment has been minimized.
This comment has been minimized.
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 ;-) |
This comment has been minimized.
This comment has been minimized.
Thanks !!! |
This comment has been minimized.
This comment has been minimized.
if you wanna handle zero values as well and display them, change the 6th line like so: |
This comment has been minimized.
This comment has been minimized.
Awesome!! +1 |
This comment has been minimized.
This comment has been minimized.
You just saved me 15 mins of my life |
This comment has been minimized.
This comment has been minimized.
Very nice! Thanks !!! |
This comment has been minimized.
This comment has been minimized.
Awesome! Thanks! |
This comment has been minimized.
This comment has been minimized.
Thank you!! |
This comment has been minimized.
This comment has been minimized.
Perfect. Thanks!!! |
This comment has been minimized.
This comment has been minimized.
Thank you so much , very helpful |
This comment has been minimized.
This comment has been minimized.
what if bytes size is 0.025 |
This comment has been minimized.
This comment has been minimized.
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
|
This comment has been minimized.
Cool and helpful.