You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 lines
370 B
JavaScript
14 lines
370 B
JavaScript
export function formatBytes( bytes, decimals = 1, units = true ) {
|
|
|
|
if ( bytes === 0 ) return '0 B';
|
|
|
|
const k = 1000;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = [ 'B', 'kB', 'MB', 'GB' ];
|
|
|
|
const i = Math.floor( Math.log( bytes ) / Math.log( k ) );
|
|
|
|
return parseFloat( ( bytes / Math.pow( k, i ) ).toFixed( dm ) ) + ( units ? ' ' + sizes[ i ] : '' );
|
|
|
|
}
|