Bird
Raised Fist0

Given a list of integers, how can you create a formatted string showing each number padded to 3 digits separated by commas?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Strings and StringBuilder
Given a list of integers, how can you create a formatted string showing each number padded to 3 digits separated by commas?
Example: [4, 15, 123] -> "004, 015, 123"
Astring.Join(", ", numbers.Select(n => $"{n:D3}"))
Bstring.Join(", ", numbers.Select(n => $"{n:F3}"))
Cstring.Join(", ", numbers.Select(n => $"{n:000}"))
Dstring.Join(", ", numbers.Select(n => $"{n:3}"))
Step-by-Step Solution
Solution:
  1. Step 1: Understand padding format D3

    :D3 pads integer with zeros to 3 digits.
  2. Step 2: Use LINQ Select with interpolation

    numbers.Select(n => $"{n:D3}") formats each number correctly.
  3. Step 3: Join with commas

    string.Join combines formatted strings with commas.
  4. Final Answer:

    string.Join(", ", numbers.Select(n => $"{n:D3}")) -> Option A
  5. Quick Check:

    Use :D3 for zero-padding integers [OK]
Quick Trick: Use :D3 to pad integers to 3 digits inside Select [OK]
Common Mistakes:
MISTAKES
  • Using :F3 which formats as float
  • Using :000 which is invalid format
  • Using :3 which does not pad with zeros

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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