Rust

format

Module

Number formatting and casts: sprintf, number_format, round/ceil, intval/floatval and the (int) / (float) casts, fmod, max/min of numbers.

Ported from PHP 8.5.11 (ext/standard/formatted_print.c, math.c, type.c, array.c, Zend/zend_operators.c, main/snprintf.c). Floating-point digits come from Rust's correctly rounded formatting, which agrees with PHP's zend_dtoa (also correctly rounded, ties to even) in every mode PHP uses here.

PHPRust
sprintfsprintf with Arg
number_formatnumber_format
round, ceil, fmodround with RoundingMode, ceil, fmod
intval, floatval, (int), (float)intval, floatval, Arg::to_int, Arg::to_float, str_to_int, str_to_float
max, min (numbers)max, min, max_array, min_array

Contents

Functions
add`$a + $b`.array_sum`array_sum($numbers)`: `+` from `0`.ceilPHP `ceil($num)`: always a float.div`$a / $b`: `None` where PHP throws `DivisionByZeroError` ("Division by zero"), for an integer or float zero divisor.dval_to_lval`zend_dval_to_lval`: a float to an integer as `(int)` converts it: NaN and infinities are 0, values outside `i64` wrap modulo 2^64 (PHP warns).floatvalPHP `floatval($value)`, the `(float)` cast: `Arg::to_float`.fmodPHP `fmod($num1, $num2)`: C `fmod` (the sign of the dividend).intvalPHP `intval($value, $base)`: `Arg::to_int` unless `value` is a string and `base` is not 10; then C `strtol` with that base, where bases 0 and 2 also accept a `0b` prefix (after whitespace and a sign).maxPHP `max($value, ...$values)` for numbers (at least two): the first largest value, keeping its type (`max(1, 1.0)` is `1`). `None` when `values` is empty.max_arrayPHP `max($array)` for an array of numbers (`zend_hash_minmax`, which compares differently from the variadic form when a NaN is involved).max_pairPHP `max($a, $b)` of two floats as a direct two-argument call (the frameless form): `$a > $b ? $a : $b`. With a NAN that is `$b`.minPHP `min($value, ...$values)` for numbers (at least two): the first smallest value, keeping its type.min_arrayPHP `min($array)` for an array of numbers.min_pairPHP `min($a, $b)` of two floats written as a direct two-argument call, which PHP 8.4+ compiles to its frameless form: `$a < $b ? $a : $b`. With a NAN that is `$b`, unlike `min` (the variadic form, `min(...$values)`), which keeps the first argument.mul`$a * $b`.number_formatPHP `number_format($num, $decimals, $decimal_separator, $thousands_separator)`: rounds half away from zero (with `round`'s pre-rounding), groups the integer digits by three and never prints `-0`. Infinite floats print `inf` and NaN `nan`, with no sign. Pass `"."` and `","` for PHP's defaults.powPHP `$base ** $exponent` (and `pow()`) of floats, or of a float and an integer: the C library's `pow`, which on the dev image (Alpine) is musl's. Ported bit for bit, so the last digit matches PHP's on every platform, where the platform's own `pow` (or a correctly rounded one) can differ.roundPHP `round($num, $precision, $mode)`: always a float. Integers with a non-negative precision are returned as they are.sprintfPHP `sprintf($format, ...$values)`: every conversion (`%s %d %u %c %e %E %f %F %g %G %h %H %o %x %X %b %%`), flags (`-`, `+`, space or `0` padding, `'c` custom padding), width, precision, `*` and `n$` argument numbers, with PHP's errors (`ValueError`, `ArgumentCountError`).str_to_float`(float) $string` / `floatval($string)` (`zend_strtod`): the leading decimal number after optional whitespace, `0.0` when there is none. Hexadecimal, `INF` and `NAN` are not numbers; `"-0"` is `-0.0`; huge exponents give infinity.str_to_int`(int) $string` / `intval($string)` (`_is_numeric_string_ex` with errors allowed): the leading integer or float after optional whitespace; floats and integers beyond `i64` saturate, non-finite floats are 0; no number is 0.sub`$a - $b`.
use utopia_php_std::format::Arg;
Rust
pub enum Arg<'a> {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(&'a [u8]),
    Array(usize),
    Object,
}

A PHP value as the formatting and conversion functions see it.

Also implements From<&'a Value>.
use utopia_php_std::format::RoundingMode;
Rust
pub enum RoundingMode {
    HalfAwayFromZero,
    HalfTowardsZero,
    HalfEven,
    HalfOdd,
    PositiveInfinity,
    NegativeInfinity,
    TowardsZero,
    AwayFromZero,
}

round()'s $mode (RoundingMode cases and PHP_ROUND_* constants).

use utopia_php_std::format::add;
Rust
pub fn add(a: Number, b: Number) -> Number

$a + $b.

use utopia_php_std::format::array_sum;
Rust
pub fn array_sum(numbers: impl IntoIterator<Item = Number>) -> Number

array_sum($numbers): + from 0.

use utopia_php_std::format::ceil;
Rust
pub fn ceil(num: Number) -> f64

PHP ceil($num): always a float.

use utopia_php_std::format::div;
Rust
pub fn div(a: Number, b: Number) -> Option<Number>

$a / $b: None where PHP throws DivisionByZeroError ("Division by zero"), for an integer or float zero divisor.

use utopia_php_std::format::dval_to_lval;
Rust
pub fn dval_to_lval(d: f64) -> i64

zend_dval_to_lval: a float to an integer as (int) converts it: NaN and infinities are 0, values outside i64 wrap modulo 2^64 (PHP warns).

use utopia_php_std::format::floatval;
Rust
pub fn floatval(value: Arg<'_>) -> f64

PHP floatval($value), the (float) cast: Arg::to_float.

use utopia_php_std::format::fmod;
Rust
pub fn fmod(num1: f64, num2: f64) -> f64

PHP fmod($num1, $num2): C fmod (the sign of the dividend).

use utopia_php_std::format::intval;
Rust
pub fn intval(value: Arg<'_>, base: i64) -> i64

PHP intval($value, $base): Arg::to_int unless value is a string and base is not 10; then C strtol with that base, where bases 0 and 2 also accept a 0b prefix (after whitespace and a sign).

use utopia_php_std::format::max;
Rust
pub fn max(values: &[Number]) -> Option<Number>

PHP max($value, ...$values) for numbers (at least two): the first largest value, keeping its type (max(1, 1.0) is 1). None when values is empty.

use utopia_php_std::format::max_array;
Rust
pub fn max_array(values: &[Number]) -> Result<Number, Error>

PHP max($array) for an array of numbers (zend_hash_minmax, which compares differently from the variadic form when a NaN is involved).

use utopia_php_std::format::max_pair;
Rust
pub fn max_pair(a: f64, b: f64) -> f64

PHP max($a, $b) of two floats as a direct two-argument call (the frameless form): $a > $b ? $a : $b. With a NAN that is $b.

use utopia_php_std::format::min;
Rust
pub fn min(values: &[Number]) -> Option<Number>

PHP min($value, ...$values) for numbers (at least two): the first smallest value, keeping its type.

use utopia_php_std::format::min_array;
Rust
pub fn min_array(values: &[Number]) -> Result<Number, Error>

PHP min($array) for an array of numbers.

use utopia_php_std::format::min_pair;
Rust
pub fn min_pair(a: f64, b: f64) -> f64

PHP min($a, $b) of two floats written as a direct two-argument call, which PHP 8.4+ compiles to its frameless form: $a < $b ? $a : $b. With a NAN that is $b, unlike min (the variadic form, min(...$values)), which keeps the first argument.

use utopia_php_std::format::mul;
Rust
pub fn mul(a: Number, b: Number) -> Number

$a * $b.

use utopia_php_std::format::number_format;
Rust
pub fn number_format(
    num: Number,
    decimals: i64,
    decimal_separator: &[u8],
    thousands_separator: &[u8],
) -> Vec<u8>

PHP number_format($num, $decimals, $decimal_separator, $thousands_separator): rounds half away from zero (with round's pre-rounding), groups the integer digits by three and never prints -0. Infinite floats print inf and NaN nan, with no sign. Pass "." and "," for PHP's defaults.

use utopia_php_std::format::pow;
Rust
pub fn pow(base: f64, exponent: f64) -> f64

PHP $base ** $exponent (and pow()) of floats, or of a float and an integer: the C library's pow, which on the dev image (Alpine) is musl's. Ported bit for bit, so the last digit matches PHP's on every platform, where the platform's own pow (or a correctly rounded one) can differ.

use utopia_php_std::format::round;
Rust
pub fn round(num: Number, precision: i64, mode: RoundingMode) -> f64

PHP round($num, $precision, $mode): always a float. Integers with a non-negative precision are returned as they are.

use utopia_php_std::format::sprintf;
Rust
pub fn sprintf(format: &[u8], values: &[Arg<'_>]) -> Result<Vec<u8>, Error>

PHP sprintf($format, ...$values): every conversion (%s %d %u %c %e %E %f %F %g %G %h %H %o %x %X %b %%), flags (-, +, space or 0 padding, 'c custom padding), width, precision, * and n$ argument numbers, with PHP's errors (ValueError, ArgumentCountError).

use utopia_php_std::format::str_to_float;
Rust
pub fn str_to_float(s: &[u8]) -> f64

(float) $string / floatval($string) (zend_strtod): the leading decimal number after optional whitespace, 0.0 when there is none. Hexadecimal, INF and NAN are not numbers; "-0" is -0.0; huge exponents give infinity.

use utopia_php_std::format::str_to_int;
Rust
pub fn str_to_int(s: &[u8]) -> i64

(int) $string / intval($string) (_is_numeric_string_ex with errors allowed): the leading integer or float after optional whitespace; floats and integers beyond i64 saturate, non-finite floats are 0; no number is 0.

Quirk kept from PHP 8.5: for a 19-digit integer followed by e+/e- and no exponent digit, PHP compares the wrong digits against PHP_INT_MIN's, so "9223372036854775808e+" is PHP_INT_MIN.

use utopia_php_std::format::sub;
Rust
pub fn sub(a: Number, b: Number) -> Number

$a - $b.