In PHP, how can the sprintf() function be used to format numbers with a specific decimal precision?
When using the sprintf() function in PHP, you can specify the decimal precision of a number by including a period followed by the number of decimal places you want to display. For example, to format a number with 2 decimal places, you would use "%.2f" in the format string. This allows you to control the precision of the output when working with numerical values.
$number = 123.456789;
$formattedNumber = sprintf("%.2f", $number);
echo $formattedNumber; // Output: 123.46
Related questions
-
PHP third-party software CMS Easy
-
array_column array_filter loop
-
PHP associative arrays key-value pairs