Method of rounding floating-point numbers by round () function in PHP _php tips

Source: Internet
Author: User

This example describes how the round () function in PHP rounds the floating-point numbers. Share to everyone for your reference. The specific methods are as follows:

Syntax: Round (X,PREC)

Parameters Describe
X Optionally, specify the number to be rounded.
Prec Optionally, specify the number of digits after the decimal point.

Description: Returns the result of rounding x according to the specified precision Prec (number of digits after the decimal point), Prec can also be negative or 0 (default).

Tips and comments

Note: PHP does not correctly handle strings similar to "12,300.2" by default.

Note: The Prec parameter was introduced in PHP 4, and the instance code is as follows:

Copy Code code as follows:
<?php
*/
Echo round (3.4); Output 3
echo "<br>";
Echo round (3.5); Output 4
echo "<br>";
Echo round (3.6); Output 4
echo "<br>";
Echo round (3.6,0); Output 4
echo "<br>";
Echo round (1.95583,2); Output 1.96
echo "<br>";
Echo round (1241757,-3); Output 1242000
echo "<br>";
Echo round (4.045,2); Output 4.05
echo "<br>";
Echo round (4.055,2); Output 4.06

Using the Stdround function to replace PHP's round function
?>


Copy Code code as follows:
<?php
function Stdround ($num, $d =0)
{
Return round ($num + 0.0001/pow ($d), $d);
}

echo "Round (1.005,2) =". Round (1.005,2). " n ";
echo "Round (1.015,2) =". Round (1.015,2). " n ";
echo "Round (1.025,2) =". Round (1.025,2). " n ";
echo "Round (1.035,2) =". Round (1.035,2). " n ";
echo "Round (1.045,2) =". Round (1.045,2). " n ";
echo "Round (1.055,2) =". Round (1.055,2). " n ";
echo "Round (1.065,2) =". Round (1.065,2). " n ";
echo "Round (1.075,2) =". Round (1.075,2). " n ";
echo "Round (1.085,2) =". Round (1.085,2). " n ";
echo "Round (1.095,2) =". Round (1.095,2). " n ";

echo "Stdround (1.005,2) =". Stdround (1.005,2). " n ";
echo "Stdround (1.015,2) =". Stdround (1.015,2). " n ";
echo "Stdround (1.025,2) =". Stdround (1.025,2). " n ";
echo "Stdround (1.035,2) =". Stdround (1.035,2). " n ";
echo "Stdround (1.045,2) =". Stdround (1.045,2). " n ";
echo "Stdround (1.055,2) =". Stdround (1.055,2). " n ";
echo "Stdround (1.065,2) =". Stdround (1.065,2). " n ";
echo "Stdround (1.075,2) =". Stdround (1.075,2). " n ";
echo "Stdround (1.085,2) =". Stdround (1.085,2). " n ";
echo "Stdround (1.095,2) =". Stdround (1.095,2). " n ";

$m = 0.000000000000001;
echo "n";
echo "Round (1.005+{$m},2) =". Round (1.005+ $m, 2). " n ";
echo "Round (1.015+{$m},2) =". Round (1.015+ $m, 2). " n ";
echo "Round (1.025+{$m},2) =". Round (1.025+ $m, 2). " n ";
echo "Round (1.035+{$m},2) =". Round (1.035+ $m, 2). " n ";
echo "Round (1.045+{$m},2) =". Round (1.045+ $m, 2). " n ";
echo "Round (1.055+{$m},2) =". Round (1.055+ $m, 2). " n ";
echo "Round (1.065+{$m},2) =". Round (1.065+ $m, 2). " n ";
echo "Round (1.075+{$m},2) =". Round (1.075+ $m, 2). " n ";
echo "Round (1.085+{$m},2) =". Round (1.085+ $m, 2). " n ";
echo "Round (1.095+{$m},2) =". Round (1.095+ $m, 2). " n ";

echo "Round (1.005,2) =". Round (1.005,2). " n ";
echo "Round (1.015,2) =". Round (1.015,2). " n ";
echo "Round (1.025,2) =". Round (1.025,2). " n ";
echo "Round (1.035,2) =". Round (1.035,2). " n ";
echo "Round (1.045,2) =". Round (1.045,2). " n ";
echo "Round (1.055,2) =". Round (1.055,2). " n ";
echo "Round (1.065,2) =". Round (1.065,2). " n ";
echo "Round (1.075,2) =". Round (1.075,2). " n ";
echo "Round (1.085,2) =". Round (1.085,2). " n ";
echo "Round (1.095,2) =". Round (1.095,2). " n ";
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.