Easy Tutorial
❮ Python Append Deepcopy Js Random ❯

PHP Rounding Methods

Category Programming Techniques

The PHP function for rounding is round(), with the syntax as follows:

float round ( float val [, int precision] )

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

Examples

Rounding numbers to two decimal places, setting a negative number:

<?php
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
?>

In addition to using the round() method, we can also use the sprintf() method to achieve rounding:

<?php
$num=0.0215489;
echo sprintf("%.3f", $num); // 0.022

$num2 = 123213.066666;
echo sprintf("%.2f", $num2); // 123213.07
?>

Reference Articles

-PHP round() Function

-PHP sprintf() Function

** Click to Share Notes

Cancel

-

-

-

❮ Python Append Deepcopy Js Random ❯