CKTIS / PHP
round()
La funzione round restituisce un float arrotondato alla precisione decimale richiesta.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero): floatLa funzione round restituisce un float arrotondato alla precisione decimale richiesta. La precisione positiva conta i decimali e quella negativa arrotonda a decine, centinaia o posizioni superiori. Le metà esatte si allontanano da zero per impostazione predefinita e HalfEven sceglie il vicino pari. PHP 8.4 accetta i casi RoundingMode e solleva ValueError per un modo intero non valido.
Firma
round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero): float
Output dell’esempio
[1.3,-1.3,2,100,"float"]
ValueError
Esempi
<?php
echo json_encode([round(1.25, 1), round(-1.25, 1), round(2.5, 0, RoundingMode::HalfEven), round(149, -2), get_debug_type(round(2))]), PHP_EOL;
try { round(2.5, 0, 999); } catch (ValueError $e) { echo get_class($e); }
Risultato
[1.3,-1.3,2,100,"float"] ValueError