The round () function in PHP rounds a floating point number.

Source: Internet
Author: User
Tags rounds
This article mainly introduces how the round () function in PHP rounds floating-point numbers. it summarizes various common usage of the round () function through detailed examples, it is a very practical technique. if you need it, you can refer to the following example to describe how the round () function in PHP rounds the floating point number. Share it with you for your reference. The specific method is as follows:

Syntax: round (x, prec)

Parameters Description
X (Optional) number to be rounded.
Prec (Optional) number of digits after the decimal point.

Note: the result of rounding x to the specified precision prec (number of digits after decimal point) is returned. The prec value can also be a negative number or zero (default value ).

Tips and comments

Note: php cannot properly process strings similar to "12,300.2" by default.

Note: The prec parameter is introduced in php 4. the instance code is as follows:

The code is as follows:

<? Php
*/
Echo round (3.4); // output 3
Echo"
";
Echo round (3.5); // output 4
Echo"
";
Echo round (3.6); // output 4
Echo"
";
Echo round (3.6, 0); // output 4
Echo"
";
Echo round (1.95583, 2); // output 1.96
Echo"
";
Echo round (1241757,-3); // output 1242000
Echo"
";
Echo round (4.045, 2); // output 4.05
Echo"
";
Echo round (4.055, 2); // output 4.06

// Use the stdround function instead of the php round function
?>



The code is as follows:

<? Php
Function stdround ($ num, $ d = 0)
{
Return round ($ num + 0.0001/pow (10, $ 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 PHP programming.

Related Article

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.