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 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.
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
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
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.
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.
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.
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.