or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

file-size.mdindex.mdnumber-formatting.mdpluralization-lists.mdstring-operations.mdutility-functions.md
tile.json

file-size.mddocs/

File Size Formatting

Human-readable file size formatting with appropriate units (bytes, KB, MB, GB, TB, PB).

Capabilities

File Size Formatting

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:

  • bytes: < 1024 bytes
  • KB: >= 1024 bytes
  • MB: >= 1024² bytes (1,048,576)
  • GB: >= 1024³ bytes (1,073,741,824)
  • TB: >= 1024⁴ bytes (1,099,511,627,776)
  • PB: >= 1024⁵ bytes (1,125,899,906,842,624)

Precision Behavior:

  • For bytes, precision is ignored (always shows whole numbers)
  • For KB and above, precision controls decimal places
  • Default precision is 2 decimal places

Deprecated Functions

filesize (DEPRECATED)

/**
 * @deprecated Use fileSize instead. Will be removed in next major version.
 */
function filesize(...args: any[]): string;