# PHP Round To 2 Decimals

Last Updated : Mar 11, 2024

In this article we will show you the solution of php round to 2 decimals, Mathematical computations, particularly rounding values to a specific number of decimal places, are a frequent chore in web development.

It is frequently necessary to round values to two decimal places in financial & other applications where accuracy is critical.

For rounding numbers to a specific number of decimal places, PHP has a number of functions.

The most popular technique for performing this process is round(). Two inputs are required by the round() function: the integer to be rounded & the number of decimal places to round to.

When an integer is rounded using the round() function, the supplied number of decimal places are added to the rounding result.

Now move to the concept of php round to 2 decimals.

## Step By Step Guide On PHP Round To 2 Decimals :-

### Method - Making use of the round() method

```<?php
\$number = 3.14159;
// Use the round function to format the number
\$formatted = round(\$number, 2);
// Output the formatted number
echo \$formatted;
?>```
1. As you can see, we have written the PHP code necessary to round a number off to two decimal points.
2. We initialise the \$number variable with the value 3.14159 at the beginning of the code.
3. The round() method, a PHP built-in function, is then called.
4. Two parameters are passed to the round() function: the first one is the value that has to be rounded off, and then the second one specifies how many decimal places should be added.
5. In this instance, \$number and 2 are passed as arguments when calling the function.
6. The value that was rounded off was then assigned to the new variable \$formatted.
7. To print the formatted number at the end, we utilise the echo statement.
8. Due to the number being rounded to two decimal places, the outcome will be 3.14.

## Method - Use the method sprintf()

```<?php
\$number = 3.14159;
\$formatted = sprintf("%0.2f", \$number);
// Output the formatted number
echo \$formatted;
?>
```
1. As you can see, the sprintf() method is used in the PHP code to format a number to two decimal places.
2. We set the value of the variable \$number to 3.14159 at the beginning of the function.
3. The sprintf() method is then used to format a string according to a defined format. It is a built-in function in PHP.
4. The format string in this instance is "%0.2f," which instructs the system to format the integer to two decimal places.
5. The value that has to be formatted is included in the variable \$number, which is the second parameter.
6. The formatted value is then assigned to a new variable named \$formatted.
7. To output the formatted number in the final step, we utilise the echo statement.
8. As the number was structured to two decimal places, the outcome will be 3.14.

## Conclusion :-

As a result, we have successfully learnt how to round PHP values to two decimal places.

Additionally, we discovered how to prepare numbers and texts for display by using the sprintf() method.

I hope this article on php round to 2 decimals helps you and the steps and method mentioned above are easy to follow and implement.