Libraries / format
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
Documentation
API Reference
format_bytes($bytes, int $precision = 2, bool $binary = false): string
Formats a non-negative byte count into a human-readable string.
$bytes (int|float): Byte count (must be >= 0).
$precision (int, default 2): Number of decimal places for units larger than bytes (0 to 20).
$binary (bool, default false): When false, uses SI base-1000 units (B, KB, MB, GB, TB, PB, EB). When true, uses IEC base-1024 units (B, KiB, MiB, GiB, TiB, PiB, EiB).
Returns: Formatted byte string (e.g. "512 B", "1.50 MB", "1.00 KiB").
format_duration($seconds, bool $short = true): string
Formats a non-negative duration in seconds into a human-readable string.
$seconds (int|float): Duration in seconds (must be >= 0). Values between 0 and 1 are formatted in milliseconds.
$short (bool, default true): When true, uses compact units (1d 2h 3m 4s, 250ms). When false, uses full English words (1 day, 2 hours, 3 minutes, 4 seconds, 250 milliseconds).
Returns: Formatted duration string.
format_number($value, int $precision = 0, string $thousandsSep = ",", string $decimalSep = "."): string
Formats a number with thousands grouping and fixed decimal precision.
$value (int|float): Numeric value to format.
$precision (int, default 0): Number of decimal places (0 to 20).
$thousandsSep (string, default ","): Thousands grouping separator.
$decimalSep (string, default "."): Decimal point separator.
Returns: Formatted number string (e.g. "1,234,567.89").
format_percent($value, int $precision = 1, bool $isRatio = true): string
Formats a number as a percentage.
$value (int|float): Numeric value.
$precision (int, default 1): Number of decimal places (0 to 20).
$isRatio (bool, default true): When true, multiplies $value by 100 (e.g. 0.755 -> "75.5%"). When false, formats $value directly (e.g. 75.5 -> "75.5%").
Returns: Formatted percentage string.
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.
$amount (int|float): Monetary amount.
$symbol (string, default "$"): Non-empty currency symbol (e.g. "$", "€", "¥").
$precision (int, default 2): Number of decimal places (0 to 20).
$thousandsSep (string, default ","): Thousands grouping separator.
$decimalSep (string, default "."): Decimal point separator.
Returns: Formatted currency string (e.g. "-$1,234.56").
Error Handling
All functions validate their inputs and throw an Exception with a clear format: ... message on invalid arguments:
format: precision must be non-negative, got -1
format: precision must be at most 20, got 25
format: value must be a number
format: bytes must be non-negative
format: duration must be non-negative
format: currency symbol must not be empty
try {
format_bytes(-10);
} catch (Exception $e) {
println($e->getMessage()); // "format: bytes must be non-negative"
}
Examples
01 Quick 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.