How to implement rounding in php without retaining decimals

藏色散人
Release: 2023-03-15 07:26:01
Original
2906 people have browsed it

php method to implement rounding without retaining decimals: 1. Round directly through the intval function and discard decimals; 2. Round up through round; 3. Round up through ceil; 4. Through floor Round down.

How to implement rounding in php without retaining decimals

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to achieve rounding without retaining in php Decimal?

Four commonly used methods for PHP integer functions:

  • intval(): round directly, discard decimals, and retain integers;

  • round(): Round to an integer;

  • ceil(): Round up, if there are decimals, add 1;

  • floor(): Round down.

  • number_format(): Function formats a number by grouping thousands.

intval() integer conversion function

int intval ( mixed $var [, int $base = 10 ] )
Copy after login

Returns the variable var by using the specified base conversion (default is decimal) integer value. intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.

echo intval(42); // 42
echo intval(4.2); // 4
echo intval('42'); // 42
Copy after login

round() function

round(number,precision,mode);
Copy after login

Rounds floating point numbers.

echo round(42.12123); // 42
echo round(42.62123); // 43
echo round(42.12123, 0); // 42
echo round(42.12123, 2); // 42.12
echo round(4212123, -2); // 421212300
Copy after login

ceil() function further method

Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up by one.

echo ceil(42.12123); // 43
echo ceil(42.62123); // 43
Copy after login

floor() function tail-removing method

Returns the next integer not greater than value, and rounds the decimal part of value.

echo floor(42.12123); // 42
echo floor(42.62123); // 42
Copy after login

number_format() Function

number_format() function formats a number by grouping by thousands.

number_format(number,decimals,decimalpoint,separator);
Copy after login

number: required. The number to format. If no other parameters are set, the number is formatted without a decimal point and with a comma (,) as the thousands separator.

decimals: optional. Specify the number of decimals. If this parameter is set, numbers are formatted using a period (.) as the decimal point.

decimalpoint: optional. Specifies the string used as the decimal point.

separator: optional. Specifies the string used as thousands separator. Only the first character of the parameter is used. For example, "xxx" only outputs "x".

echo number_format("1000000"); // 1,000,000
echo number_format("1000000",2); // 1,000,000.00
echo number_format("1000000",2,",","."); // 1.000.000,00
echo number_format("1000000",2,"*","."); // 1.000.000*00
echo number_format("1000000",2,".",""); // 1000000.00
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement rounding in php without retaining decimals. 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