Human-readable file size formatting with appropriate units (bytes, KB, MB, GB, TB, PB).
Formats file sizes in bytes to human-readable format with appropriate binary units.
/**
* Formats the value like a 'human-readable' file size
* Uses binary prefixes (1024-based): bytes, KB, MB, GB, TB, PB
* @param filesize - Size in bytes
* @param precision - Number of decimal places (default: 2)
* @returns Human-readable file size string
*/
function fileSize(filesize: number, precision?: number): string;Usage Examples:
const Humanize = require('humanize-plus');
// Small files (bytes)
console.log(Humanize.fileSize(0)); // "0 bytes"
console.log(Humanize.fileSize(1)); // "1 byte"
console.log(Humanize.fileSize(512)); // "512 bytes"
// Kilobytes
console.log(Humanize.fileSize(1024)); // "1 KB"
console.log(Humanize.fileSize(1536)); // "2 KB" (rounded)
// Megabytes
console.log(Humanize.fileSize(1024 * 1024)); // "1.00 MB"
console.log(Humanize.fileSize(2.22 * 1024 * 1024)); // "2.22 MB"
// Gigabytes
console.log(Humanize.fileSize(2.22 * 1024 * 1024 * 1024)); // "2.22 GB"
// Terabytes
console.log(Humanize.fileSize(2.22 * 1024 * 1024 * 1024 * 1024)); // "2.22 TB"
// Petabytes
console.log(Humanize.fileSize(2.22 * 1024 * 1024 * 1024 * 1024 * 1024)); // "2.22 PB"
// Custom precision
console.log(Humanize.fileSize(2.2222 * 1024 * 1024, 3)); // "2.222 MB"
console.log(Humanize.fileSize(2.22 * 1024 * 1024, 0)); // "2 MB"Size Unit Thresholds:
Precision Behavior:
/**
* @deprecated Use fileSize instead. Will be removed in next major version.
*/
function filesize(...args: any[]): string;