Skip to main content

Module format

Module format 

Source
Expand description

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

Enums§

Arg
A PHP value as the formatting and conversion functions see it.
RoundingMode
round()’s $mode (RoundingMode cases and PHP_ROUND_* constants).

Functions§

add
$a + $b.
array_sum
array_sum($numbers): + from 0.
ceil
PHP 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).
floatval
PHP floatval($value), the (float) cast: Arg::to_float.
fmod
PHP fmod($num1, $num2): C fmod (the sign of the dividend).
intval
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).
max
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.
max_array
PHP max($array) for an array of numbers (zend_hash_minmax, which compares differently from the variadic form when a NaN is involved).
max_pair
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.
min
PHP min($value, ...$values) for numbers (at least two): the first smallest value, keeping its type.
min_array
PHP min($array) for an array of numbers.
min_pair
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.
mul
$a * $b.
number_format
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.
pow
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.
round
PHP round($num, $precision, $mode): always a float. Integers with a non-negative precision are returned as they are.
sprintf
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).
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.