What are some functions in PHP that can help with formatting numbers?

When working with numbers in PHP, it is often necessary to format them in a specific way, such as adding commas as thousand separators or rounding to a certain number of decimal places. PHP provides several built-in functions to help with number formatting, such as number_format(), round(), and sprintf(). These functions can be used to easily format numbers according to your requirements.

// Example of using number_format() to add commas as thousand separators
$number = 1234567.89;
$formatted_number = number_format($number);
echo $formatted_number; // Output: 1,234,567

// Example of using round() to round a number to 2 decimal places
$number = 123.456789;
$rounded_number = round($number, 2);
echo $rounded_number; // Output: 123.46

// Example of using sprintf() to format a number with leading zeros
$number = 7;
$formatted_number = sprintf("%02d", $number);
echo $formatted_number; // Output: 07