format

Human-readable formatters for byte sizes, durations, numbers, percentages and currency.

v1.0.0Text & stringsbundledimport "format";
Overview Installation Documentation Examples

Overview

Human-readable formatters for byte sizes, durations, thousands-separated numbers, percentages, and currency in HolyPHP.

bundled — works offline bytes duration currency

Quick start

import "format";

println(format_bytes(1500000));             // "1.50 MB"
println(format_bytes(1048576, 1, true));    // "1.0 MiB"
println(format_duration(3661));             // "1h 1m 1s"
println(format_duration(3661, false));      // "1 hour, 1 minute, 1 second"
println(format_number(1234567.89, 2));      // "1,234,567.89"
println(format_percent(0.755));             // "75.5%"
println(format_currency(-1234.5));          // "-$1,234.50"

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install format

This adds the library to hphp.yaml, installs it into .hphp/packages/format/ and pins the exact version in hphp.lock:

require:
  format: "^1.0"

Use it

import "format";

format is bundled with the compiler, so import "format"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install format          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install format
Packageformat
Version1.0.0 (follows the compiler)
Entry fileformat.hphp
Sourcepackages/lib/format ↗

Documentation

API Reference

format_bytes($bytes, int $precision = 2, bool $binary = false): string

Formats a non-negative byte count into a human-readable string.

format_duration($seconds, bool $short = true): string

Formats a non-negative duration in seconds into a human-readable string.

format_number($value, int $precision = 0, string $thousandsSep = ",", string $decimalSep = "."): string

Formats a number with thousands grouping and fixed decimal precision.

format_percent($value, int $precision = 1, bool $isRatio = true): string

Formats a number as a percentage.

format_currency($amount, string $symbol = "$", int $precision = 2, string $thousandsSep = ",", string $decimalSep = "."): string

Formats a monetary amount with a currency symbol and thousands separators, placing any minus sign before the symbol.

Error Handling

All functions validate their inputs and throw an Exception with a clear format: ... message on invalid arguments:

try {
    format_bytes(-10);
} catch (Exception $e) {
    println($e->getMessage()); // "format: bytes must be non-negative"
}

Examples

01Quick start

Bundled with the compiler — import "format"; works offline in any file, or add it to a project with hphp install format.

import "format";

println(format_bytes(1500000));             // "1.50 MB"
println(format_bytes(1048576, 1, true));    // "1.0 MiB"
println(format_duration(3661));             // "1h 1m 1s"
println(format_duration(3661, false));      // "1 hour, 1 minute, 1 second"
println(format_number(1234567.89, 2));      // "1,234,567.89"
println(format_percent(0.755));             // "75.5%"
println(format_currency(-1234.5));          // "-$1,234.50"
Try it
hphp install format
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_format.hphp.