Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

round()

Use round to produce a float at the requested decimal precision.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero): float

Use round to produce a float at the requested decimal precision. Positive precision counts places right of the decimal point and negative precision rounds to tens, hundreds or larger places. The default rounds exact halves away from zero; HalfEven instead chooses the even neighbor. PHP 8.4 accepts RoundingMode cases and rejects invalid integer modes with ValueError.

Signature

round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero): float

Example output

[1.3,-1.3,2,100,"float"]
ValueError

Examples

<?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); }

Result

[1.3,-1.3,2,100,"float"]
ValueError

Sources