philippn / number-formatter
A lightweight PHP number formatter for bytes, durations, and money.
Requires
- php: >=8.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-02 17:19:22 UTC
README
A lightweight PHP library for rounding numbers, formatting byte counts and durations, and displaying monetary amounts without a currency symbol. Requires PHP 8.0 or later.
Installation
Install with Composer:
composer require philippn/number-formatter
Usage
Load Composer’s autoloader and import the formatter:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Philippn\NumberFormatter\NumberFormatter;
NumberFormatter::bytesTo(1024, 'KB'); // "1.00 KB"
NumberFormatter::formatBytes(1536); // "1.50 KB"
NumberFormatter::formatDuration(3661); // "1 hr 1 min 1 sec"
NumberFormatter::money('1234.56'); // "1 234.56"
NumberFormatter::money('1.236'); // "~1.24"
Byte formatting
bytesTo($bytes, $unit = 'MB', $precision = 2)converts a byte count to the requested unit. Unit names are case-insensitive. An unsupported unit throwsInvalidArgumentException.formatBytes($bytes, $precision = 2)selects a unit automatically. Counts below 1,024 are formatted inB, regardless of precision.
Both methods accept integers, finite floats, and numeric strings. Decimal and scientific notation can be passed directly as floats or quoted as strings. For example, 1.6, 1.536e3, 1024.0, '1.6', and '1.536e3' are valid. Strings with unit suffixes or other nonnumeric text, such as '1024KB', are rejected.
Both methods throw InvalidArgumentException for unsupported types, nonnumeric strings, non-finite floats, negative counts, and strings that convert to infinity or underflow from a nonzero value to zero. Signed zero values, including -0.0 and '-0.0', are treated as zero.
Both methods return strings and use powers of 1,024 with the labels B, KB, MB, GB, TB, and PB. By default, they display exactly two decimal places for KB through PB, including trailing zeros. Exact values have no prefix. If rounding changes the value, the result starts with ~. If a positive value would round to zero, the result instead starts with < followed by the smallest displayable value, such as <0.01 MB. Only an actual zero can display as 0.00.
The minimum adjusts to the requested precision (for example, 0.001 at precision 3). The B unit never displays decimal places: a positive count that would round to zero is shown as <1 B, and a fractional count that rounds to a nonzero value is marked with ~.
| Call | Output |
|---|---|
bytesTo(1) | <0.01 MB |
bytesTo(6, 'KB') | ~0.01 KB |
bytesTo(104952000) | ~100.09 MB |
bytesTo(0, 'GB') | 0.00 GB |
bytesTo(1, 'MB', 3) | <0.001 MB |
bytesTo(42, 'B') | 42 B |
bytesTo('0.4', 'B') | <1 B |
bytesTo('+1.024E+3', 'KB') | 1.00 KB |
bytesTo(1536.0, 'KB') | 1.50 KB |
formatBytes('4e-1') | <1 B |
formatBytes(1.6) | ~2 B |
formatBytes('1.536e3') | 1.50 KB |
formatBytes(1025) | ~1.00 KB |
Duration formatting
formatDuration(int $seconds) formats non-negative whole seconds using years, months, weeks, days, hours, minutes, and seconds. It uses the largest applicable unit first and omits units with a zero count. Inputs from 0 to 59 display in seconds only. Negative durations throw InvalidArgumentException.
Units have fixed lengths: a day is 24 hours, a week is 7 days, a month is 30 days, and a year is 365 days. The formatter does not use calendar dates or account for leap years.
| Call | Output |
|---|---|
formatDuration(0) | 0 sec |
formatDuration(59) | 59 sec |
formatDuration(60) | 1 min |
formatDuration(75) | 1 min 15 sec |
formatDuration(3600) | 1 hr |
formatDuration(86_400) | 1 day |
formatDuration(604_800) | 1 week |
formatDuration(2_592_000) | 1 month |
formatDuration(31_536_000) | 1 year |
formatDuration(34_822_861) | 1 year 1 month 1 week 1 day 1 hr 1 min 1 sec |
Money formatting
money($amount) accepts integers, finite floats, and numeric strings. Decimal and scientific notation can be passed directly as floats or quoted as strings. For example, 12.34, 1.234e1, 12.0, '12.34', and '1.234e1' are valid. Currency codes and other nonnumeric text, such as '12.34 USD', are rejected.
Non-finite floats, unsupported types, nonnumeric strings, negative amounts, and strings that convert to infinity or underflow from a nonzero value to zero throw InvalidArgumentException. Signed zero values, including -0.0 and '-0.0', are treated as zero.
The method returns a string with spaces as thousands separators and a period as the decimal separator. Zero displays as 0.00. Amounts of 0.01 or more display with two decimal places. Positive amounts below 0.01 are rounded to three decimal places. A ~ prefix indicates that rounding changed the amount, except when a sub-cent amount rounds to zero or to one cent.
| Input | Output |
|---|---|
0 | 0.00 |
1234 | 1 234.00 |
0.033066 | ~0.03 |
'0.0004' | <0.001 |
'0.001' | 0.001 |
'0.0019' | ~0.002 |
'0.0099' | 0.01 |
'1.236' | ~1.24 |
'+1.23456E+3' | 1 234.56 |
'1.9e-3' | ~0.002 |
'3.0e-5' | <0.001 |
Tests
Run the smoke tests and duration formatting tests from the project root:
composer test
License
MIT