What are the different methods available in PHP to limit the decimal places in a calculation result?

When performing calculations in PHP, you may encounter situations where you need to limit the decimal places in the result. This can be achieved using various methods such as using the number_format() function, sprintf() function, or by manually rounding the result to the desired number of decimal places.

// Method 1: Using number_format() function
$number = 123.456789;
$rounded_number = number_format($number, 2); // Limit to 2 decimal places
echo $rounded_number;

// Method 2: Using sprintf() function
$number = 123.456789;
$rounded_number = sprintf("%.2f", $number); // Limit to 2 decimal places
echo $rounded_number;

// Method 3: Manually rounding the result
$number = 123.456789;
$rounded_number = round($number, 2); // Limit to 2 decimal places
echo $rounded_number;