Format Numbers with number_format()
Write a PHP function that takes a number and returns it formatted with 2 decimal places and commas as thousands separators using number_format(). For example, 1234.5 becomes "1,234.50".
number_format() is a PHP built-in function that formats a number with grouped thousands. It can take 1, 2, or 4 parameters: the number, decimal places, decimal separator, and thousands separator. With default parameters, it uses comma for thousands and period for decimals.
The number_format() function is the standard way to display currency, large numbers, and statistical data in PHP applications. It handles rounding automatically to the specified decimal places and works with both integer and float inputs.
Time complexity is O(1). The function is implemented in C internally and is highly optimized.
Example Input & Output
Small number
Zero formatted
Large number
Negative number
Format with thousands separator
Algorithm Flow

Solution Approach
Best Answers
<?php
function solution($num) {
return number_format($num, 2);
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
