Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

round

(PHP 4, PHP 5, PHP 7)

roundRounds a float

Description

round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float

Returns the rounded value of val to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

Note: PHP doesn't handle strings like "12,300.2" correctly by default. See converting from strings.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

val

The value to round.

precision

The optional number of decimal digits to round to.

If the precision is positive, the rounding will occur after the decimal point.

If the precision is negative, the rounding will occur before the decimal point. If the absolute value of the precision is greater than or equal to the number of digits, the result of the rounding is equal to 0

mode

Use one of the following constants to specify the mode in which rounding occurs.

Constants Description
PHP_ROUND_HALF_UP Round val up to precision decimal places away from zero, when it is half way there. Making 1.5 into 2 and -1.5 into -2.
PHP_ROUND_HALF_DOWN Round val down to precision decimal places towards zero, when it is half way there. Making 1.5 into 1 and -1.5 into -1.
PHP_ROUND_HALF_EVEN Round val to precision decimal places towards the nearest even value.
PHP_ROUND_HALF_ODD Round val to precision decimal places towards the nearest odd value.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

The value rounded to the given precision as a float.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 round() examples

<?php
var_dump
(round(3.4));
var_dump(round(3.5));
var_dump(round(3.6));
var_dump(round(3.60));
var_dump(round(1.955832));
var_dump(round(1241757, -3));
var_dump(round(5.0452));
var_dump(round(5.0552));
?>

The above example will output:

float(3)
float(4)
float(4)
float(4)
float(1.96)
float(1242000)
float(5.05)
float(5.06)

Example #2 How precision affects a float

<?php
$number 
1346.21;

var_dump(round($number2));
var_dump(round($number1));
var_dump(round($number0));
var_dump(round($number, -1));
var_dump(round($number, -2));
var_dump(round($number, -3));
var_dump(round($number, -4));
?>

The above example will output:

float(1346.21)
float(1346.2)
float(1346)
float(1350)
float(1300)
float(1000)
float(0)

Example #3 mode examples

<?php
echo 'Rounding modes with 9.5' PHP_EOL;
var_dump(round(9.50PHP_ROUND_HALF_UP));
var_dump(round(9.50PHP_ROUND_HALF_DOWN));
var_dump(round(9.50PHP_ROUND_HALF_EVEN));
var_dump(round(9.50PHP_ROUND_HALF_ODD));

echo 
'Rounding modes with 8.5' PHP_EOL;
var_dump(round(8.50PHP_ROUND_HALF_UP));
var_dump(round(8.50PHP_ROUND_HALF_DOWN));
var_dump(round(8.50PHP_ROUND_HALF_EVEN));
var_dump(round(8.50PHP_ROUND_HALF_ODD));
?>

The above example will output:

Rounding modes with 9.5
float(10)
float(9)
float(10)
float(9)
Rounding modes with 8.5
float(9)
float(8)
float(8)
float(9)

Example #4 mode with precision examples

<?php
echo 'Using PHP_ROUND_HALF_UP with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_UP));
var_dump(round1.541PHP_ROUND_HALF_UP));
var_dump(round(-1.551PHP_ROUND_HALF_UP));
var_dump(round(-1.541PHP_ROUND_HALF_UP));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_DOWN));
var_dump(round1.541PHP_ROUND_HALF_DOWN));
var_dump(round(-1.551PHP_ROUND_HALF_DOWN));
var_dump(round(-1.541PHP_ROUND_HALF_DOWN));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_EVEN));
var_dump(round1.541PHP_ROUND_HALF_EVEN));
var_dump(round(-1.551PHP_ROUND_HALF_EVEN));
var_dump(round(-1.541PHP_ROUND_HALF_EVEN));

echo 
PHP_EOL;
echo 
'Using PHP_ROUND_HALF_ODD with 1 decimal digit precision' PHP_EOL;
var_dump(round1.551PHP_ROUND_HALF_ODD));
var_dump(round1.541PHP_ROUND_HALF_ODD));
var_dump(round(-1.551PHP_ROUND_HALF_ODD));
var_dump(round(-1.541PHP_ROUND_HALF_ODD));
?>

The above example will output:

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float(1.6)
float(1.5)
float(-1.6)
float(-1.5)

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float(1.5)
float(1.5)
float(-1.5)
float(-1.5)

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float(1.6)
float(1.5)
float(-1.6)
float(-1.5)

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float(1.5)
float(1.5)
float(-1.5)
float(-1.5)

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Changelog

Version Description
5.3.0 The mode parameter was introduced.
5.2.7 The inner workings of round() was changed to conform to the C99 standard.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

See Also

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-function.round.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut