How to use round() Function In PHP
The round() function is a built-in PHP function that rounds a number to the nearest integer or a specified number of decimal places.
PHP
<?php $number = 3.14159; // Round to 2 decimal places $roundNumber = round ( $number , 2); echo "Original Number: " . $number . "\n" ; echo "Rounded Number: " . $roundNumber ; ?> |
Output
Original Number: 3.14159 Rounded Number: 3.14
How to Round Number to n Decimal Places in PHP ?
Rounding numbers to a specific decimal place is a common operation in programming, especially when dealing with precision in mathematical calculations. PHP provides several approaches to achieve this rounding.
Table of Content
- Using round() Function
- Using number_format() Function
- Using sprintf() Function