1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-09-28 15:50:10 +02:00

fix: confusion between GB and GiB

This commit is contained in:
Elias Schneider 2023-07-22 15:29:53 +02:00
parent 890588f5da
commit 5816b39fc6
No known key found for this signature in database
GPG Key ID: 07E623B294202B6C

View File

@ -1,17 +1,17 @@
export function byteToHumanSizeString(bytes: number) { export function byteToHumanSizeString(bytes: number) {
const sizes = ["B", "KB", "MB", "GB", "TB"]; const sizes = ["B", "KB", "MB", "GB", "TB"];
if (bytes == 0) return "0 Byte"; if (bytes == 0) return "0 Byte";
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString()); const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)).toString());
return (bytes / Math.pow(1024, i)).toFixed(1).toString() + " " + sizes[i]; return (bytes / Math.pow(1000, i)).toFixed(1).toString() + " " + sizes[i];
} }
export function byteToUnitAndSize(bytes: number) { export function byteToUnitAndSize(bytes: number) {
const units = ["B", "KB", "MB", "GB", "TB"]; const units = ["B", "KB", "MB", "GB", "TB"];
if (bytes == 0) return { unit: "B", size: 0 }; if (bytes == 0) return { unit: "B", size: 0 };
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString()); const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)).toString());
return { return {
size: parseFloat((bytes / Math.pow(1024, i)).toFixed(1)), size: parseFloat((bytes / Math.pow(1000, i)).toFixed(1)),
unit: units[i], unit: units[i],
}; };
} }
@ -19,5 +19,5 @@ export function byteToUnitAndSize(bytes: number) {
export function unitAndSizeToByte(unit: string, size: number) { export function unitAndSizeToByte(unit: string, size: number) {
const units = ["B", "KB", "MB", "GB", "TB"]; const units = ["B", "KB", "MB", "GB", "TB"];
const i = units.indexOf(unit); const i = units.indexOf(unit);
return Math.pow(1024, i) * size; return Math.pow(1000, i) * size;
} }