Bird
0
0

Which of the following is the correct syntax to print a floating-point number with 2 decimal places using printf in PHP?

easy📝 Syntax Q12 of 15
PHP - Output and String Handling
Which of the following is the correct syntax to print a floating-point number with 2 decimal places using printf in PHP?
Aprintf("%.2f", $number);
Bprintf("%2f.", $number);
Cprintf("%f.2", $number);
Dprintf("%f2.", $number);
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct decimal precision syntax

    In PHP, to format a float with 2 decimals, use %.2f where .2 specifies 2 decimal places.
  2. Step 2: Check each option's format string

    Only printf("%.2f", $number); uses %.2f correctly; others have misplaced or incorrect syntax.
  3. Final Answer:

    printf("%.2f", $number); -> Option A
  4. Quick Check:

    %.2f = float with 2 decimals [OK]
Quick Trick: Use %.Nf to print float with N decimals [OK]
Common Mistakes:
  • Placing decimal after %f instead of before
  • Using %2f instead of %.2f
  • Adding extra characters inside format string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes