Skip to main content
Home

@std/fmt@1.0.8
Built and signed on GitHub Actions

Utilities for formatting values, such as adding colors to text, formatting durations, printf utils, formatting byte numbers.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (1.0.8)
function format
format(
num: number,
options?: FormatOptions,
): string

Convert bytes to a human-readable string: 1337 → 1.34 kB

Based on pretty-bytes. A utility for displaying file sizes for humans.

Examples

Basic usage

import { format } from "@std/fmt/bytes";
import { assertEquals } from "@std/assert";

assertEquals(format(1337), "1.34 kB");
assertEquals(format(100), "100 B");

Include bits representation

import { format } from "@std/fmt/bytes";
import { assertEquals } from "@std/assert";

assertEquals(format(1337, { bits: true }), "1.34 kbit");

Include sign

import { format } from "@std/fmt/bytes";
import { assertEquals } from "@std/assert";

assertEquals(format(42, { signed: true }), "+42 B");
assertEquals(format(-42, { signed: true }), "-42 B");

Change locale

import { format } from "@std/fmt/bytes";
import { assertEquals } from "@std/assert";

assertEquals(format(1337, { locale: "de" }), "1,34 kB");

Parameters

The bytes value to format

optional
options: FormatOptions

The options for formatting

Return Type

The formatted string

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/fmt

Import symbol

import { format } from "@std/fmt/bytes";
or

Import directly with a jsr specifier

import { format } from "jsr:@std/fmt/bytes";

Add Package

pnpm i jsr:@std/fmt
or (using pnpm 10.8 or older)
pnpm dlx jsr add @std/fmt

Import symbol

import { format } from "@std/fmt/bytes";

Add Package

yarn add jsr:@std/fmt
or (using Yarn 4.8 or older)
yarn dlx jsr add @std/fmt

Import symbol

import { format } from "@std/fmt/bytes";

Add Package

npx jsr add @std/fmt

Import symbol

import { format } from "@std/fmt/bytes";

Add Package

bunx jsr add @std/fmt

Import symbol

import { format } from "@std/fmt/bytes";