Rounding a number in any programming language can be performed to an integer or to a specified number of decimal places. In addition, it is also taken into account in which direction the number is rounded - to a higher or lower value, as well as mathematically to the nearest side.

PHP uses the following functions to round numbers:

- round - rounding to the nearest value;

- ceil - rounding up to the nearest value;

- floor - rounds down to the nearest value.

Rounding to whole number

All functions use a floating point value as the main parameter. For instance:

$ x = 123.4393;

When executing each function, we get the following values:

echo ceil ($ x); // 124 (to a larger integer value)
echo floor ($ x); // 123 (to a smaller integer value)
echo round ($ x); // 123 (mathematically to the closest)

For mathematical rounding, the value of the number of the part being rounded is taken into account. If it is less than 5, it is rounded down. If equal to or greater than 5, rounding will be performed upward. In this case, the round function rounds down to a lower value, since the value of the fractional part is less than 0.5. If you use a larger value, for example:

$ x = 123.5;

As a result of rounding, we get:

echo round ($ x); // 124

Rounding a number to a specified number of decimal places

For such rounding of numbers, only the round function is used, which has a second (optional) parameter that determines the rounding precision. By asking it, you can get the required number of decimal places:

$ x = 123.8393;
echo round ($ x, 1); // 123.8
echo round ($ x, 2); // 123.84
echo round ($ x, 3); // 123.839

Other rounding examples

If you suddenly need to implement a more exotic rounding - to a larger or smaller value with a specified number of decimal places, additional operations will be required. As mentioned above, the ceil and floor functions have only one parameter and will not be able to round a number to the specified precision.

In such cases, you can multiply the number so that you can round it up later. For example, a number needs to be rounded to a larger value with two decimal places:

$ x = 123.8313;
echo ceil ($ x * 100) / 100; // 123.84

As a result, we get 123.84, although the same rounding function in this case would return 123.83.

You can do the same if you need to round to a smaller value with a given precision:

$ x = 123.8393;
echo floor ($ x * 100) / 100; // 123.83

Good evening friends, today we continue to study PHP language... The topic that we will study today is called rounding numbers in PHP... Sometimes, as a result of performing some functions, you get a result that is not an integer, and your task is to get an integer. The first example that comes to mind is page generation on your site. And three functions come to your aid in solving such a problem: round, ceil, floor.
Let's talk about each function separately. The first round function rounds a floating point number. As parameters of the function, it can take just a number that will be rounded, and as the second parameter, we can specify a number that will mean the number of digits after the decimal point after rounding.
The second ceil function, allows you to round up a fraction; as a parameter, only a number is specified for further rounding.
And the last one floor function, opposite ceil, and rounds down, and also takes one parameter. Now let's look at each function with an example.

echo round ( 7.123 ). "
" ;

echo round ( 7.492, 1 ). "
" ;

echo round ( 7.92, 0 ). "
" ;

echo "
" ;

echo ceil ( 9.3 ). "
" ;

echo ceil ( 9.8 ). "
" ;

echo "
" ;

echo floor ( 4.2 ). "
" ;

echo floor ( 4.7 ). "
" ;

?>

As you can see, everything is quite simple to use, just look at the result of these functions and you will no longer have any questions.


So I think commenting is superfluous, and at this point we will complete the study of the topic. rounding numbers in PHP... And for today I say goodbye to you, see you soon.


Friends!
We're in the middle of a big cleanup!
Errors and page curvature possible
Seeing everything and quickly is not possible!
If you find some mistake, then if it's not difficult for you to write the address ...
You are here now:

http: //site/page/php/function/050_okruglit_tseloe_chislo_v_menshuyu_storonu_php.html

Round down an integer Php.

Our next task is to round down an integer, and always! Even if there is not enough one to one number in front and trailing zeros ...!

Most likely it is not clear ..., for example, we have the number 24 999, it needs to be rounded down. That we will have 20,000

We have functions that can round down an integer, but only until the number is in the middle ...

We need to round the number 24 999

Round (24999, -4);

And we get:

But as soon as we cross the middle of 25,000. This function rounds up.

I tried to find a function or some options and in general I could not find anything! But we must somehow get out of the situation! And moreover, we have a task. So that this function rounds any number if it is more than 10 downwards!

What should be done!? Have to take

It's that simple!

And we will get exactly what is required!

Result:

20000 Online rounding to the smallest integer number

In order to check the rounding down of a number using Php - try rounding any number except one-digit ...