How to divide and round in php

coldplay.xixi
Release: 2023-03-04 08:20:02
Original
5501 people have browsed it

Methods for division and rounding in php: 1. Use the [round()] function to round floating point numbers; 2. Use the [ceil()] function to round up to the nearest integer; 3. Use The [floor()] function rounds down to the nearest integer.

How to divide and round in php

How to divide and round in php:

1. round: rounding

round()The function rounds floating point numbers.

How to divide and round in php

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

Tip: PHP cannot handle strings like "12,300.2" correctly by default.

Example:

<?php
echo(round(0.60));
echo(round(0.50));
echo(round(0.49));
echo(round(-4.40));
 echo(round(-4.60));
 ?>
Copy after login

Output:

1 1
2 1
3 0
4 -4
5 -5
Copy after login

2. ceil: round up

ceil() function rounds up is the nearest integer.

How to divide and round in php

Description: Return the next integer that is not less than x. If x has a decimal part, it will be rounded up by one. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

Example:

 <?php
 echo(ceil(0.60);
 echo(ceil(0.40);
 echo(ceil(5);
echo(ceil(5.1);
 echo(ceil(-5.1);
 echo(ceil(-5.9));
 ?>
Copy after login

Output:

1 1
2 1
3 5
4 6
5 -5
6 -5
Copy after login

3. floor: round down

floor() function down Rounded to the nearest integer.

Syntax: floor(x)

How to divide and round in php

Description: Return the next integer not greater than x, and round off the decimal part of x. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.

Example:

<?php
echo(floor(0.60));
echo(floor(0.40));
echo(floor(5));
echo(floor(5.1));
echo(floor(-5.1));
 echo(floor(-5.9))
 ?>
Copy after login

Output:

1 0
2 0
3 5
4 5
5 -6
6 -6
Copy after login

Related learning recommendations: php programming (video)

The above is the detailed content of How to divide and round in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template