What are the methods of rounding in PHP?

藏色散人
Release: 2021-01-11 10:42:15
Original
15394 people have browsed it

This article mainly introduces you to the four methods of PHP rounding.

The problem of rounding in PHP is not only encountered when we learn PHP, but it is also one of the common test points during our PHP interview process.

Below we will summarize and introduce the four methods of PHP rounding with simple examples.

The first method: round directly, discard the decimals, and keep the integers

<?php
$num = 3.1415926;
$num2 = 3.6;

echo intval($num).&#39;<br>&#39;;
echo  intval($num2);
echo "<hr>";
Copy after login

The result is as follows:

What are the methods of rounding in PHP?

The second method: rounding

<?php
$num = 3.1415926;
$num2 = 3.6;

echo round($num).&#39;<br>&#39;;
echo  round($num2);
echo "<hr>";
Copy after login

The result is as follows:

What are the methods of rounding in PHP?

Three methods: Round up

<?php
$num = 3.1415926;
$num2 = 3.6;

echo ceil($num).&#39;<br>&#39;;
echo  ceil($num2);
echo "<hr>";
Copy after login

The result is as follows:

What are the methods of rounding in PHP?

The fourth method: Round down

<?php
$num = 3.1415926;
$num2 = 3.6;

echo floor($num).&#39;<br>&#39;;
echo  floor($num2);
Copy after login

The results are as follows:

What are the methods of rounding in PHP?

Related function introduction:

intvalFunction representation Gets the integer value of a variable.

roundThe function represents rounding of floating point numbers.

ceilThe function represents further rounding.

floorThe function represents rounding by rounding.

This article is about the four methods of PHP rounding. It is very simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of What are the methods of rounding in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!