Round decimal places in PHP

How to round to any number of decimal places in PHP. How to round to two decimal places and how to format numbers in PHP so that they always show you the two decimal places, useful when working with currencies.

Among PHP’s mathematical functions is one that allows us to round a float (floating point number or number with decimals) to the nearest integer value. This is the round() function. To explain its use, it is best to see it with a couple of examples:

round(0.6); // would return the integer value 1. round(7.3); // would return the integer value 7.

On many occasions we will need to round a floating point number so that we preserve some decimal places. For example, if we are doing operations with euros or dollars, the logical thing is that we keep two decimal places in the rounding. Oddly enough, PHP does not include a rounding function that allows rounding while preserving decimals, and that is why we are going to discuss a way to achieve it in this article.

UPDATED: The PHP round function now allows you to specify the number of decimal places you want to use for rounding. That’s why in this article we will see a quick and direct way to do it with PHP using the same round function. We will also explain the “artisanal” way to achieve it, by hand with a function created by ourselves.

Round two decimal places with the round function

The fastest way to achieve this rounding with PHP is to use the round function itself. In the second parameter we can indicate the number of decimal places that we want to apply as precision to the rounding.

See also  HTML layout for jQuery calendar

round(0.2234, 2); // would return the numeric value 0.22 round(7.3, 2); // would return the numeric value 7.3 round(5, 2); // would return the integer value 5

Format the number to always get two decimal places

As you can see that the rounding function does not add the decimals in case the value does not have them. Note that if you want it to always show you a value with two decimal places, then you have to use the number_format() function, for example like this:

number_format(4, 2, ‘,’, ‘.’); // would return 4.00

The first value is the amount we want with the correct decimal places. The second value is the number of decimal places, in this case 2. The third value is the character to separate the integer part of the decimal and the fourth value is the thousands separator. As you can imagine, this is a very useful trick when working with coins.

Rounding with two decimal places in an artisan way

We are going to use a little trick to round to two decimal places, using the PHP round() function. It is about doing this simple mathematical operation.

Given a value, first we are going to multiply it by 100, we are going to round it and finally, we are going to divide the result again by 100. The case, in an example, is this:

For the value 2.7931. First we multiply it by 100. We have the value 279.31. Now we do the rounding, since the 0.31 we want to get rid of. We would do a round(279.31) that returns 279. Finally, we divide this value by 100, to recover the decimals, which gives us 2.79, which is the value we wanted to obtain.

See also  What is and how to dispose of Xdebug

The formula we get would be the following:

round(float_value * 100) / 100

If we want, we can use a function like this to round up to two decimal places:

function round_two_decimal($value) { $float_rounded=round($value * 100) / 100; return $float_rounded; }

For a set of values ​​like this:

round_two_decimal(3.56666) round_two_decimal(3.008) round_two_decimal(3.66) round_two_decimal(3.199) round_two_decimal(3.9999)

It would give us the following result:

3.57

3.01

3.66

3.2

4

Round to any number of decimal places

Following the guidelines set in the previous example, we are going to create a function to round a float to any desired number of decimal places.

It is about applying the same logic above. To round to two decimal places we have seen that we had to multiply by 100, round and then divide again by 100. If we want to round with a single decimal, we will have to multiply by 10, round and then divide by 10 again.

Let’s do an example of rounding to three decimal places. We have the value 0.65195. Since we want to keep 3 decimal places, we must multiply by 1000, which gives us 651.95. Now we do the rounding with the round() function, which leaves the number without decimals at 652 (it is rounded to the nearest integer). Finally, we need to divide the value by 1000 again, giving us 0.652.

The rule then is that we have to multiply and then divide, by the power of 10 with the number of decimal places we want to keep. To obtain powers in PHP we have the pow() function, which receives the base and exponent of the power sought.

See also  Creating a dropdown menu in Dreamweaver

For example, pow(10,3) returns 1000 and pow(10,1) returns 10.

With this function in PHP we can obtain the rounding to the desired decimal places.

function rounded($number, $decimal places) { $factor = pow(10, $decimal places); return (round($number*$factor)/$factor); }

If we call it with a set of values ​​like these:

rounded (0.00211.3) rounded (8.56.2) rounded (7.26.1) rounded (211.3) rounded (2009.2)

We will obtain the following results:

0.002

8.56

7.3

211

2.01

We hope you find this couple of features useful. We must thank Héctor Alonso for the second of the functions shown in this article.

References: We also recommend reading the FAQ, where the PHP function number_format() is explained in detail, which, apart from rounding decimals, allows you to format the number so that it shows the separator you want in thousands and decimals.

If we work with ASP, we have another article to learn how to .

Loading Facebook Comments ...
Loading Disqus Comments ...