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

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations format.*).

Contents

2 submodules, and what the module defines itself.

use php_std::format::Arg;
Rust
pub enum Arg<'a>

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

Also implements From.
use php_std::format::RoundingMode;
Rust
pub enum RoundingMode

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

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

PHP ceil($num): always a float.

use 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 php_std::format::floatval;
Rust
pub fn floatval(value: Arg<'_>) -> f64

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

use 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 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 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 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 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 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 php_std::format::min_array;
Rust
pub fn min_array(values: &[Number]) -> Result<Number, Error>

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

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

format::arith

Own page

PHP's arithmetic operators on numbers (Zend/zend_operators.c): integers that overflow become floats, / gives an integer only when the division is exact, and dividing by zero throws.

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

$a + $b.

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

array_sum($numbers): + from 0.

use php_std::format::arith::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 php_std::format::arith::mul;
Rust
pub fn mul(a: Number, b: Number) -> Number

$a * $b.

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

$a - $b.

format::pow

Own page

pow() as musl computes it (src/math/pow.c, from Arm's optimized routines, MIT licensed): PHP's ** on floats calls the C library's pow, and the dev image is Alpine. This is not correctly rounded (the worst case is 0.54 ULP), so a correctly rounded or platform pow differs from PHP's in the last digit for some inputs. The fused multiply-add paths are the ones musl takes where the target has FMA (__FP_FAST_FMA, aarch64).