Bird
Raised Fist0

Which of the following is the correct syntax for formatting a double value to show two decimal places using string interpolation in C#?

easy📝 Syntax Q12 of Q15
C Sharp (C#) - Strings and StringBuilder
Which of the following is the correct syntax for formatting a double value to show two decimal places using string interpolation in C#?
Adouble price = 9.99; string s = $"Price: {price:0.00}";
Bdouble price = 9.99; string s = $"Price: {price,2}";
Cdouble price = 9.99; string s = $"Price: {price:.2f}";
Ddouble price = 9.99; string s = $"Price: {price.ToString("0.00")}";
Step-by-Step Solution
Solution:
  1. Step 1: Recognize C# format specifier syntax

    In C#, inside interpolation braces, :0.00 formats numbers to two decimals.
  2. Step 2: Check each option's correctness

    double price = 9.99; string s = $"Price: {price:0.00}"; uses correct format {price:0.00}. double price = 9.99; string s = $"Price: {price,2}"; uses comma which is for alignment, not decimals. double price = 9.99; string s = $"Price: {price:.2f}"; uses Python style .2f which is invalid in C#. double price = 9.99; string s = $"Price: {price.ToString("0.00")}"; calls ToString inside interpolation but with escaped quotes incorrectly.
  3. Final Answer:

    double price = 9.99; string s = $"Price: {price:0.00}"; -> Option A
  4. Quick Check:

    Format decimals with :0.00 inside {} [OK]
Quick Trick: Use colon and format code inside braces for formatting [OK]
Common Mistakes:
MISTAKES
  • Using Python or other language format codes
  • Confusing alignment comma with format colon
  • Trying to call methods inside interpolation incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes