Created
March 19, 2013 03:05
-
-
Save lanqy/5193417 to your computer and use it in GitHub Desktop.
JavaScript To Convert Bytes To MB, KB, Etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/ | |
function bytesToSize(bytes) { | |
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | |
if (bytes == 0) return 'n/a'; | |
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); | |
if (i == 0) return bytes + ' ' + sizes[i]; | |
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i]; | |
}; |
Using the Intl.NumberFormat
for local friendly conversions
export function bytesToSize(bytes: number): string {
const units = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte'];
const navigatorLocal = navigator.languages && navigator.languages.length >= 0 ? navigator.languages[0] : 'en-US'
const unitIndex = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1));
return Intl.NumberFormat(navigatorLocal, {
style: 'unit',
unit : units[unitIndex]
}).format(bytes / (1024 ** unitIndex))
}
The sizes should be named Mebibyte (MiB), Gibibyte (GiB) etc if you use 1024 for the calculation.
Or if you want to calculate Megabyte, Gigabyte etc. replace the '1024' in the code by '1000'.
@HaukeHa Thank you for noticing 👍
I use the following code:
export function convertBytes(bytes: number, options: { useBinaryUnits?: boolean; decimals?: number } = {}): string {
const { useBinaryUnits = false, decimals = 2 } = options;
if (decimals < 0) {
throw new Error(`Invalid decimals ${decimals}`);
}
const base = useBinaryUnits ? 1024 : 1000;
const units = useBinaryUnits
? ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
: ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(base));
return `${(bytes / Math.pow(base, i)).toFixed(decimals)} ${units[i]}`;
}
and basic tests:
import { describe, expect } from "vitest";
import { convertBytes } from "./bytes";
describe("convertBytes", () => {
const fileSizeInBytes = 2048;
describe("default conversion", () => {
it("should convert to KB", () => {
const resultKB = convertBytes(fileSizeInBytes);
expect(resultKB).toBe("2.05 KB");
});
it("should convert to MB", () => {
const resultMB = convertBytes(fileSizeInBytes * 1000);
expect(resultMB).toBe("2.05 MB");
});
it("should throw an error for invalid decimals", () => {
expect(() => convertBytes(fileSizeInBytes, { decimals: -1 })).toThrowError("Invalid decimals -1");
});
});
describe("conversion to binary units", () => {
it("should convert to KiB", () => {
const resultKiB = convertBytes(fileSizeInBytes, { useBinaryUnits: true });
expect(resultKiB).toBe("2.00 KiB");
});
it("should convert to MiB", () => {
const resultMiB = convertBytes(fileSizeInBytes * 1024, { useBinaryUnits: true });
expect(resultMiB).toBe("2.00 MiB");
});
});
it("should handle very large bytes (Number.MAX_SAFE_INTEGER)", () => {
const result = convertBytes(Number.MAX_SAFE_INTEGER);
expect(result).toBe("9.01 PB");
});
});
const units = ["byte", "kilobyte", "megabyte", "terabyte", "petabyte"];
you lost "gigabyte"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To expand on the typescript version, I don't think the
parseInt(...)
is required. Also, in the unlikely case that you exceed999.9 TB
, you end up inxxx.x undefined
territory.Here is a fixed Typescript version:
With Jest spec: