How can sprintf() be used to format numbers with a specific number of decimal places in PHP?
To format numbers with a specific number of decimal places in PHP, you can use the sprintf() function. This function allows you to specify the number of decimal places by using a format specifier like "%.2f" for two decimal places. Simply pass the format specifier along with the number you want to format to sprintf() to achieve the desired result.
$number = 123.456789;
$formattedNumber = sprintf("%.2f", $number);
echo $formattedNumber; // Output: 123.46