PHP rounding, rounding, keeping two decimal places, etc. A complete list of number processing methods

不言
Release: 2023-03-24 14:02:02
Original
12217 people have browsed it

This article introduces the content of PHP rounding, rounding, retaining two decimal places, and other number processing methods. It has certain reference value. Now I share it with you. Friends in need can refer to it

There are four commonly used methods for PHP rounding functions. The four functions are collected below; rounding functions are often used. Let’s briefly summarize them today! In fact, it is very simple, just a few functions~~The main ones are: ceil, floor, round, intval

1. ceil — Enter One-way rounding

Explanation

float ceil (float value)

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

Example: ceil()

<?php
 
echo ceil(4.3); // 5
echo ceil(9.999); // 10
 
?>
Copy after login

2. Floor - Rounding by rounding off method

Explanation

float floor (float value )

Returns the next integer not greater than value, and rounds the decimal part of value. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.

Example: floor()

<?php
 
echo floor(4.3); // 4
echo floor(9.999); // 9
 
?>
Copy after login

3. round - round floating point numbers

Instructions:

float round ( float val [, int precision] )

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

Example: round()

<?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
 
?>
Copy after login

4. intval—convert the variable into an integer type

例子:intval()

<?php
 
echo intval(4.3); //4
echo intval(4.6); // 4
 
?>
Copy after login

相关推荐:

PHP函数之十进制、二进制、八进制和十六进制的转换

The above is the detailed content of PHP rounding, rounding, keeping two decimal places, etc. A complete list of number processing methods. 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!