PHP round() Functie
Voorbeeld
Ronde getallen:
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
Definitie en gebruik
De functie round() rondt een getal met drijvende komma af.
Tip: Om een getal naar boven af te ronden op het dichtstbijzijnde gehele getal, kijkt u naar de functie ceil() .
Tip: Om een getal OMLAAG af te ronden op het dichtstbijzijnde gehele getal, kijkt u naar de functie floor() .
Syntaxis
round(number,precision,mode);
Parameterwaarden
Parameter | Description |
---|---|
number | Required. Specifies the value to round |
precision | Optional. Specifies the number of decimal digits to round to. Default is 0 |
mode | Optional. Specifies a constant to specify the rounding mode:
|
Technische details
Winstwaarde: | De afgeronde waarde |
---|---|
Retourtype: | Vlot |
PHP-versie: | 4+ |
PHP-wijzigingslogboek: | PHP 5.3: De modusparameter is toegevoegd |
Meer voorbeelden
Voorbeeld
Rond getallen af op twee decimalen:
<?php
echo(round(4.96754,2) . "<br>");
echo(round(7.045,2) . "<br>");
echo(round(7.055,2));
?>
Voorbeeld
Ronde getallen met behulp van de constanten:
<?php
echo(round(1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_UP) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_DOWN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_EVEN) . "<br>");
echo(round(1.5,0,PHP_ROUND_HALF_ODD) . "<br>");
echo(round(-1.5,0,PHP_ROUND_HALF_ODD));
?>
❮ PHP-wiskundereferentie