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.
| PHP | Rust |
|---|---|
sprintf | sprintf with Arg |
number_format | number_format |
round, ceil, fmod | round 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.
- Rounding
Mode round()’s$mode(RoundingModecases andPHP_ROUND_*constants).
Functions§
- add
$a + $b.- array_
sum array_sum($numbers):+from0.- ceil
- PHP
ceil($num): always a float. - div
$a / $b:Nonewhere PHP throwsDivisionByZeroError(“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 outsidei64wrap modulo 2^64 (PHP warns).- floatval
- PHP
floatval($value), the(float)cast:Arg::to_float. - fmod
- PHP
fmod($num1, $num2): Cfmod(the sign of the dividend). - intval
- PHP
intval($value, $base):Arg::to_intunlessvalueis a string andbaseis not 10; then Cstrtolwith that base, where bases 0 and 2 also accept a0bprefix (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)is1).Nonewhenvaluesis 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, unlikemin(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 (withround’s pre-rounding), groups the integer digits by three and never prints-0. Infinite floats printinfand NaNnan, with no sign. Pass"."and","for PHP’s defaults. - pow
- PHP
$base ** $exponent(andpow()) of floats, or of a float and an integer: the C library’spow, 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 ownpow(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 or0padding,'ccustom padding), width, precision,*andn$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.0when there is none. Hexadecimal,INFandNANare not numbers;"-0"is-0.0; huge exponents give infinity.- str_
to_ int (int) $string/intval($string)(_is_numeric_string_exwith errors allowed): the leading integer or float after optional whitespace; floats and integers beyondi64saturate, non-finite floats are 0; no number is 0.- sub
$a - $b.