PHP Round up | How to round up value in PHP with Example?
Summary: in this article, you will learn about PHP Round up with an example and How to round the value in PHP with Example? Let’s understand and look at rounding numbers with using the following function such as-
- round()
- number_format()
- ceil()
- floor()
- s/printf()
round()
The PHP round() function is used to rounds the number passed into the specified number of decimal places. If the decimal place is a negative number then the numbers to the left of the decimal place are rounded. furthermore, if the decimal places parameter isn’t passed in then it defaults to 0(zero) and will round as an integer. five is round up and < 5 is rounded down.
1 2 3 4 5 6 7 |
<?php echo round(244.752, 2); // 244.75 echo round(244.751, 1); // 244.8 echo round(244.751); // 245 echo round(244.751, -1); // 240 echo round(244.751, -2); // 200 ?> |
From PHP 5.3.0 round() accepts a 3rd parameter can be passed that specifies the rounding mode and is one of
- PHP_ROUND_HALF_EVEN
- PHP_ROUND_HALF_ODD
- PHP_ROUND_HALF_UP
- PHP_ROUND_HALF_DOWN
It defaults to PHP_ROUND_HALF_UP to keep up or maintain backward compatibility.
PHP number_format()
The number_format() function in PHP is used for formatting numbers with decimal places and thousands of separators, but it also rounds numbers if there are more decimal places in the original number than required. such as round(), it’ll round-up on five(5) and down on < 5.
1 2 3 4 5 |
<?php echo number_format(254.759); // 255 echo number_format(254.759, 1); // 254.8 echo number_format(254.759, 2); // 254.76 ?> |
ceil()
The ceil() function in PHP rounds-up to the nearest integer (i.e. ceil = ceiling). There’s no argument for precision thus it’ll always round up to the nearest integer. If the number is already an integer then the value returned will remain the equivalent or the same.
1 2 3 4 5 6 |
<?php echo ceil(362.453); // 363 echo ceil(362.453); // 363 echo ceil(-362.453); // -362 echo ceil(-362.453); // -362 ?> |
floor()
The PHP floor() function works the same way as ceil() but always rounding down to the nearest integer. let’s see some examples:
1 2 3 4 5 6 |
<?php echo floor(364.251); // 364 echo floor(364.751); // 364 echo floor(-364.251); // -365 echo floor(-364.751); // -365 ?> |
printf() and sprintf()
The printf() and sprintf() functions in PHP are also for formatting and again will round numbers as well. printf() outputs the value to standard output whereas sprintf() returns it as a value.
1 2 3 4 5 |
<?php printf("%0.2f", 364.862); // 364.86 printf("%0.1f", 364.862); // 364.9 printf("%d", 364.862); // 364 ?> |
PHP round up Related Question and Answer:
Question1: Do you round up at 5 or 6?
Answer: Here’s also we follow the round-up general rule for rounding: If the number you’re rounding is followed by 5, 6, 7, 8, or 9, round the number up. for Example such as 39 rounded to the nearest ten is 40. furthermore, If the number you’re rounding is followed by 0, 1, 2, 3, or 4, round the number down.
Question2: How do I round to 2 decimal places in PHP?
Answer: for the round to 2 decimal places in PHP, you can use the PHP number_format() function. This will show exactly two digits after the decimal point. The main Advantage: If you would like to display two digits after a float value only and not for int, then use this.
PHP round up conclusion:
In this article, you have learned round-up different function, which is used to round the numbers with example. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.