Home  >  Article  >  Backend Development  >  How to achieve non-retaining decimals in php

How to achieve non-retaining decimals in php

藏色散人
藏色散人Original
2021-02-24 18:01:312949browse

php method to achieve non-retaining decimals: 1. Use the floor function to round down to the nearest integer. The syntax is such as "floor(x)", which will return the next integer not greater than x. The decimal part is rounded off; 2. Use the ceil function to round up to the nearest integer. The syntax is such as "ceil(x)", which will return the next integer that is not less than x. If x has a decimal part, it will be rounded up. .

How to achieve non-retaining decimals in php

The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer

php There are two ways to achieve non-retaining decimals:

#1: The floor() function rounds down to the nearest integer.

Syntax

floor(x)

Parameters

  • x Required. A number.

Explanation

  • Returns the next integer not greater than x, and rounds 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:

In this example, we will apply the floor() function to different numbers:

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

Output:

0
0
5
5
-6
-6

2: The ceil() function rounds up to the nearest integer.

Syntax

ceil(x)

Parameters

  • x Required. A number.

Explanation

  • Returns 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:

In this example, we will apply the ceil() function to different values:

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

Output:

1
1
5
6
-5
-5

[Recommended learning: "PHP Video Tutorial"]

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