Roll back changes to conversion unit (1000->1024)

closes #4183
This commit is contained in:
DaneEveritt 2022-06-27 18:46:36 -04:00
parent 1b5d77dc17
commit 72f545259f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 29 additions and 21 deletions

View file

@ -1,4 +1,4 @@
const _CONVERSION_UNIT = 1000;
const _CONVERSION_UNIT = 1024;
/**
* Given a value in megabytes converts it back down into bytes.
@ -9,13 +9,16 @@ function mbToBytes(megabytes: number): number {
/**
* Given an amount of bytes, converts them into a human readable string format
* using "1000" as the divisor.
* using "1024" as the divisor.
*/
function bytesToString(bytes: number): string {
function bytesToString(bytes: number, decimals = 2): string {
const k = _CONVERSION_UNIT;
if (bytes < 1) return '0 Bytes';
const i = Math.floor(Math.log(bytes) / Math.log(_CONVERSION_UNIT));
const value = Number((bytes / Math.pow(_CONVERSION_UNIT, i)).toFixed(2));
decimals = Math.floor(Math.max(0, decimals));
const i = Math.floor(Math.log(bytes) / Math.log(k));
const value = Number((bytes / Math.pow(k, i)).toFixed(decimals));
return `${value} ${['Bytes', 'KB', 'MB', 'GB', 'TB'][i]}`;
}