lbry-desktop/ui/util/format-bytes.js

13 lines
425 B
JavaScript
Raw Permalink Normal View History

2020-08-07 22:59:20 +02:00
// @flow
export function formatBytes(bytes: number, decimals?: number = 2) {
if (bytes === 0) return __('0 Bytes');
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [__('Bytes'), __('KB'), __('MB'), __('GB'), __('TB'), __('PB'), __('EB'), __('ZB'), __('YB')];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}