What are some best practices for handling decimal numbers and formatting them correctly in PHP for various output formats?

When handling decimal numbers in PHP, it is important to ensure that they are formatted correctly for various output formats. This includes rounding numbers to a specific decimal place, displaying them with the correct number of decimal places, and formatting them with appropriate separators such as commas. One way to achieve this is by using PHP's number_format() function, which allows you to specify the number of decimal places, decimal point, and thousands separator.

// Format a decimal number with two decimal places and comma as thousands separator
$number = 12345.6789;
$formatted_number = number_format($number, 2, '.', ',');
echo $formatted_number; // Output: 12,345.68