php

How to Round Number to 2 Decimal Places in PHP

There are various methods in PHP to accomplish rounding numbers, including built-in functions or more complex code developed by the developer. Regardless of whether you are working with financial data or aiming to display numbers in a specific format, rounding numbers to two decimal places becomes crucial in such circumstances.

Our focus in this article is to examine different techniques for rounding numbers to 2 decimal places using PHP.

Round Number to 2 Decimal Places in PHP

There are several methods to round numbers to 2 Decimal places in PHP:

1: Using round() Function

PHP offers a round() function that enables you to round any figure to a specific number of decimal places. This function demands two parameters: the initial number to be rounded and the desired decimal precision. Omitting the second argument will cause the system to round off to the nearest integer.

In PHP, the syntax of the round() function is:

round(float $number, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float

Here, $number is the number to be rounded, $precision is the number of decimal places to round to (default is 0), and $mode specifies the rounding mode (default is PHP_ROUND_HALF_UP). The function returns the rounded value as a float.

For example:

<?php

$num = 6.3456;

$rounded_num = round($num, 2);

echo "The rounded number is: " . $rounded_num;

?>

2: Using number_format() Function

The number_format() function in PHP is another built-in function that lets you format a given number with a group of thousands and decimal places. It is crucial when you want to format the output of a calculation as an integer.

The following is the syntax to use number_format() in PHP:

number_format(number, decimal_place, decimal_separator, thousand_separator);

The first number parameter is the number you want to format. The second parameter, decimal_place, determines the precision of the floating-point number to which the original variable will be converted in PHP. The third parameter decimal_separator specifies the characters to use as the decimal separator. The fourth parameter Thousand_separator specifies the characters used as the thousands separator.

For example:

<?php

$number = 15.672342;

$formatted_number = number_format($number, 2, '.', ',');

echo "The rounded number is: " . $formatted_number;

?>

3: Using sprintf() Function

You can also use the sprintf() function to round a number to 2 decimal places in PHP and the syntax for the sprintf() function is given as:

sprintf(format, arg1, arg2, ...)

Here, the format is the format string containing placeholders for the arguments, and arg1, arg2, …. are the arguments to be formatted. The function returns the formatted string.

For example:

<?php

$num = 12.34567;

$rounded_num = sprintf("%.2f", $num);

echo "The rounded number is: ". $rounded_num;

?>

Conclusion

Converting numbers to 2 decimal places is one of the common tasks for developers working in programming languages. This article focuses on various ways to do this using PHP, including functions such as round(), number_format(), and sprintf(). Each method is explained with an example for better understanding.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.