Home  >  Article  >  Backend Development  >  How to implement rounding in php without retaining decimals

How to implement rounding in php without retaining decimals

藏色散人
藏色散人Original
2022-01-28 10:11:162881browse

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 ] )

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

round() function

round(number,precision,mode);

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

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

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

number_format() Function

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

number_format(number,decimals,decimalpoint,separator);

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

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!

Statement:
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