Round numbers using the constants in PHP

Description

The following code shows how to round numbers using the constants.

Example


//from www  .java2  s .c o m
<?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));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Development »




Environment
Error
Hash
Include
Locale
Math
Network
Output
Reflection
PHP Regular Expressions