How can the NumberFormatter class in PHP be utilized to format numbers with specific decimal places and thousand separators?

To format numbers with specific decimal places and thousand separators in PHP, you can use the NumberFormatter class. You can create an instance of NumberFormatter with the desired locale and formatting style, then use the format() method to format the numbers accordingly. By specifying the number of decimal places and setting the thousand separator style, you can customize the output as needed.

$number = 123456.789;
$formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
$formatter->setAttribute(NumberFormatter::GROUPING_USED, true);

echo $formatter->format($number);