# Numbers (laravel-rust-format)

> Use laravel_rust_format_core::number for Laravel’s Illuminate\Support\Number helpers in English. Use php::number_format for PHP’s separate half-away-from-zero…

- Package: laravel-rust-format (Laravel for Rust, alpha), crates: `laravel-rust-format`, `laravel-rust-format-core`
- Maintenance: active; Latest release: 0.1.1 (2026-12-04); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-format), [crates.io](https://crates.io/crates/laravel-rust-format/0.1.1), [docs.rs](https://docs.rs/laravel-rust-format/0.1.1), [Changelog](https://oss.bowerbird.si/format/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-format/blob/16e2c1a95c60fb32c5ee2f37b7d58a9f72be23f7/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-format/blob/16e2c1a95c60fb32c5ee2f37b7d58a9f72be23f7/SECURITY.md)
- Based on: [Laravel Str, Number and Pluralizer](https://github.com/laravel/framework): The behaviour of Illuminate\Support\Str, Number and Pluralizer, as typed Rust functions with English defaults.; [Carbon](https://github.com/briannesbitt/Carbon): The calendar, formats and relative-time translations, on Jiff with an explicit clock instant. The generated locale tables credit the authors of each language file.; [Doctrine Inflector](https://github.com/doctrine/inflector): English inflection rules, generated into Rust tables.; [Portable ASCII](https://github.com/voku/portable-ascii): Transliteration tables for Str::ascii and Str::slug, generated into Rust tables.; [League CommonMark](https://github.com/thephpleague/commonmark): Reference behaviour for Markdown in the PHP oracle; Comrak renders Markdown in Rust.; [Ramsey UUID](https://github.com/ramsey/uuid): Reference behaviour for identifiers in the PHP oracle; the core builds them in Rust.; [Symfony UID](https://github.com/symfony/uid): Reference behaviour for identifiers in the PHP oracle; the core builds them in Rust.
- PHP parity: core (465 covered, 4 diverged, 3 planned of 472 abilities; 133,206 fixtures; follows illuminate/support 13.39.0; verified 2026-12-04)
- Used in production across multiple projects.
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/format/docs/numbers/
- Source: https://github.com/bowerbird-si/laravel-rust-format/blob/16e2c1a95c60fb32c5ee2f37b7d58a9f72be23f7/docs/numbers.md

Use `laravel_rust_format_core::number` for Laravel’s `Illuminate\Support\Number` helpers in English. Use [`php::number_format`](https://oss.bowerbird.si/format/docs/php-functions/) for PHP’s separate half-away-from-zero formatter. The English decimal formatter follows ICU’s half-even rounding.

## Integers, floats and precision

Functions accept `impl Into<PhpNumber>`. Integers in the signed 64-bit range keep their digits. Wider integers outside that range convert to `f64`. To accept numeric text from PHP data, parse it with `PhpNumber::from_numeric_str`.

```rust
use laravel_rust_format_core::{number, number::Precision, PhpNumber};

assert_eq!(PhpNumber::from_numeric_str("9007199254740993"), Some(PhpNumber::Int(9007199254740993)));
assert_eq!(PhpNumber::from_numeric_str(" 1.5e3 "), Some(PhpNumber::Float(1500.0)));
assert_eq!(PhpNumber::from_numeric_str("12abc"), None);

assert_eq!(number::format(1234.56789), "1,234.568");
assert_eq!(number::format_with(12.3, Precision::Exact(2)), "12.30");
assert_eq!(number::format_with(12.3, Precision::Max(2)), "12.3");
assert_eq!(number::format_with(5.125, Precision::Max(2)), "5.12");
```

Use `Precision::Exact(n)` for PHP’s `precision` argument and `Precision::Max(n)` for `maxPrecision`. If PHP supplied both, use `Max`. The English decimal default is up to three fractional digits.

## Units, currencies and ordinals

```rust
use laravel_rust_format_core::number::{self, IcuData, Precision};

assert_eq!(number::file_size(1000), "1 KB");
assert_eq!(number::file_size_with(2560, Precision::Max(2)), "2.5 KB");
assert_eq!(number::percentage(50, 1), "50.0%");
assert_eq!(number::abbreviate(999.5, 0), "1K");
assert_eq!(number::for_humans_with(1200, Precision::Max(2), false), "1.2 thousand");
assert_eq!(number::ordinal(-1), "−1st");
assert_eq!(
    number::currency_with(-1234.5, "CHF", IcuData::Icu76, Some(1)).as_deref(),
    Some("-CHF\u{a0}1,234.5"),
);
```

| Helper | Behavior |
| --- | --- |
| `file_size`, `file_size_with` | Scales by 1024 and switches units when the next magnitude exceeds 0.9. Defaults to zero fractional digits. |
| `percentage`, `percentage_with` | Takes percentage points. Pass `50` for fifty percent and `0` for the PHP default precision. |
| `abbreviate`, `abbreviate_with` | Uses compact units such as `K`. Rounding can promote the unit. |
| `for_humans`, `for_humans_with` | Spells out units. The last argument of `for_humans_with` selects abbreviated labels when true. |
| `ordinal` | The number and its suffix are chosen separately, as in ICU. Negative ordinals can use the Unicode minus sign U+2212. |
| `currency`, `currency_with` | Returns `Option<String>`. PHP returns `false` for a rejected code. An empty code selects USD. With no precision override, the currency determines how many digits to show. |

`currency_with` lets you select the ICU data version: `IcuData::Icu72` (the default, matching production's PHP 8.4 / ICU 72.1) or `IcuData::Icu76`. Currency amounts go through `f64`, as in PHP’s `formatCurrency()`. Keep non-breaking spaces intact when comparing currency strings.

## Range helpers

```rust
use laravel_rust_format_core::{number, PhpNumber::{Float, Int}};

assert_eq!(number::trim(12.0), Int(12));
assert_eq!(number::clamp(25, 0, 10.0), Float(10.0));
assert_eq!(number::pairs(10, 5).unwrap(), [[Int(0), Int(4)], [Int(5), Int(9)], [Int(10), Int(10)]]);
```

`trim` converts whole floats to integers when they would round-trip that way through PHP JSON. `clamp` keeps the type of the selected argument. `pairs` creates a list of inclusive ranges, starting at zero with an offset of one. Use `pairs_with(to, by, start, offset)` for other bounds. The step uses its magnitude. Zero is an error, as is a step too small to advance the current bound at floating-point precision.

## Localized numbers, spellout and parsing

Import the functions from `laravel_rust_format_core::number`. Pass an explicit `IcuData` when output must match a particular PHP runtime.

```rust
use laravel_rust_format_core::number::{
    IcuData, Locale, Precision, parse_int, spell, spell_ordinal, spell_with,
};

assert_eq!(spell(-21.05), "minus twenty-one point zero five");
assert_eq!(spell_ordinal(123), "one hundred twenty-third");
assert_eq!(spell_with(10, Some(2), Some(10)), "10");
assert_eq!(parse_int("−1,234.50 kg"), Some(-1234));
assert_eq!(parse_int("12,34"), None);

let french = Locale::new("fr", IcuData::Icu76).unwrap().with_currency("EUR");
assert_eq!(french.format(1234.5, None), "1\u{202f}234,5");
assert_eq!(french.percentage(12.5, Precision::Exact(1)), "12,5\u{a0}%");
assert_eq!(french.currency(1234.5, None, None).as_deref(), Some("1\u{202f}234,50\u{a0}€"));

let india = Locale::new("en_IN", IcuData::Icu76).unwrap();
assert_eq!(india.format(1234567.5, None), "12,34,567.5");
assert_eq!(india.spell(100000, None, None).as_deref(), Ok("one lakh"));
```

Pass `None` to `Locale::format` for ICU's locale defaults. `Precision::Exact(n)` pads with trailing zeros and `Precision::Max(n)` caps the fractional digits. Pass `None` to `Locale::currency` for its currency's usual precision. A percent input is already a percentage, so 12.5 displays as 12.5 percent.

`Locale::file_size`, `abbreviate` and `for_humans` use localized numeric punctuation and Laravel’s unit labels.

```rust
use laravel_rust_format_core::number::{IcuData, Locale, Precision};
let french = Locale::new("fr", IcuData::Icu76).unwrap();
assert_eq!(french.file_size(2560, Precision::Exact(1)), "2,5 KB");
assert_eq!(french.abbreviate(1250, 0, Some(2)), "1,25K");
assert_eq!(french.for_humans(1250, 2, None), "1,25 thousand");
```

In `abbreviate` and `for_humans`, a supplied maximum takes precedence when formatting. The base precision still controls whether Laravel uses a localized zero or the literal `"0"`. Use `Limits::pairs_with` to check the number of pairs and their storage size before allocation.

Construct a context per locale and keep it for a request or closure. `with_currency` returns a copy with a different default currency. An explicit code in `Locale::currency` overrides that context's currency. A PHP-empty code (the empty string or `"0"`) uses its default.

Parsing accepts a valid numeric prefix, including Unicode decimal digits and ICU's grouping equivalents. Leading whitespace and inconsistent group widths can cause parsing to return `None`. This is PHP's `false`. `parse_int` truncates toward zero and checks signed 64-bit overflow. `parse_float` preserves signed zero. Use `Locale::parse` with `ParseType::Int32`, `Int64` or `Float` to choose a type. The explicit PHP type values `null` and `NumberFormatter::TYPE_DEFAULT` raise `ValueError` on both tested runtimes.

```rust
use laravel_rust_format_core::number::{IcuData, Locale};
let french = Locale::new("fr", IcuData::Icu76).unwrap();
assert_eq!(french.parse_float("1 234,50 kg"), Some(1234.5));
assert_eq!(french.parse_int("12 34"), None);
assert_eq!(french.spell(30, None, Some(50)).as_deref(), Ok("trente"));
assert_eq!(french.ordinal(1).as_deref(), Ok("1er"));
let german = Locale::new("de", IcuData::Icu76).unwrap();
assert_eq!(german.spell_ordinal(12).as_deref(), Ok("zwölfte"));
```

The `after` and `until` cutoffs select ordinary locale decimal formatting at either boundary and outside the spelling interval. Some locales use ordinary spellout for `spell_ordinal` because ICU does not expose a public `%spellout-ordinal` rule set there. Enable the core crate's `number-locale-all` feature to include spellout and ordinal rule sets for every bundled locale. Localized parsing is available across the decimal locale tables by default.

Community package, not affiliated with Laravel.
