Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Format specifiers for numbers and dates in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to format the number as currency.

C Sharp (C#)
double price = 1234.56;
string formatted = price.ToString("[1]");
Console.WriteLine(formatted);
Drag options to blanks, or click blank then click option'
AD
BC
CF
DN
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'D' which is for decimal integers, not currency.
Using 'F' which formats as fixed-point number without currency symbol.
2fill in blank
medium

Complete the code to format the date as year-month-day.

C Sharp (C#)
DateTime today = new DateTime(2024, 6, 15);
string formatted = today.ToString("[1]");
Console.WriteLine(formatted);
Drag options to blanks, or click blank then click option'
AMM/dd/yyyy
Bdd-MM-yyyy
Cyyyy-MM-dd
DMMMM dd, yyyy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MM/dd/yyyy' which formats as month/day/year.
Using 'dd-MM-yyyy' which puts day first.
3fill in blank
hard

Fix the error in the code to format a number with two decimal places.

C Sharp (C#)
double value = 3.14159;
string result = value.ToString("[1]");
Console.WriteLine(result);
Drag options to blanks, or click blank then click option'
AF2
BN
CD2
DC2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'D2' which is for decimal integers, not floating numbers.
Using 'N' without specifying decimals may show default decimals.
4fill in blank
hard

Fill both blanks to format the date as full month name and 24-hour time.

C Sharp (C#)
DateTime dt = new DateTime(2024, 6, 15, 18, 30, 0);
string formatted = dt.ToString("[1] [2]");
Console.WriteLine(formatted);
Drag options to blanks, or click blank then click option'
AMMMM
BHH:mm
Chh:mm tt
Dddd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hh:mm tt' which is 12-hour time with AM/PM.
Using 'ddd' which shows abbreviated day name.
5fill in blank
hard

Fill all three blanks to create a dictionary with word lengths for words longer than 3 letters.

C Sharp (C#)
string[] words = {"apple", "bat", "carrot", "dog"};
var lengths = new Dictionary<string, int>()
{
    {"[1]", [2]
};

foreach (var word in words)
{
    if (word.Length [3] 3)
    {
        lengths[word] = word.Length;
    }
}

foreach (var kvp in lengths)
{
    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
Drag options to blanks, or click blank then click option'
Aword
Bword.Length
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which selects words shorter than 3 letters.
Using 'word.Length' as key which is incorrect.

Practice

(1/5)
1. Which format specifier in C# is used to display a number as currency with the correct symbol and two decimal places?
decimal price = 123.456m;
easy
A. "N"
B. "C"
C. "P"
D. "D"

Solution

  1. Step 1: Understand currency format specifier

    The "C" format specifier converts a number to a currency string with the currency symbol and two decimals by default.
  2. Step 2: Compare other specifiers

    "N" formats as number with commas, "P" as percentage, "D" as decimal integer. Only "C" fits currency.
  3. Final Answer:

    "C" -> Option B
  4. Quick Check:

    Currency format = "C" [OK]
Hint: Currency format specifier is always "C" [OK]
Common Mistakes:
  • Using "N" which formats number with commas but no currency symbol
  • Using "P" which formats as percentage
  • Using "D" which is for integers only
2. Which of the following is the correct syntax to format a DateTime variable date to show only the year as four digits in C#?
easy
A. date.ToString("yy")
B. date.ToString("Year")
C. date.ToString("YYYY")
D. date.ToString("yyyy")

Solution

  1. Step 1: Identify correct year format specifier

    In C#, "yyyy" outputs the full four-digit year, while "yy" outputs two digits.
  2. Step 2: Check case sensitivity and keywords

    "YYYY" is invalid (case sensitive), and "Year" is not a recognized format string.
  3. Final Answer:

    date.ToString("yyyy") -> Option D
  4. Quick Check:

    Year four-digit format = "yyyy" [OK]
Hint: Use lowercase "yyyy" for full year, uppercase is invalid [OK]
Common Mistakes:
  • Using uppercase "YYYY" which is invalid
  • Using "yy" which shows only two digits
  • Using non-format strings like "Year"
3. What is the output of the following C# code?
double value = 0.7567;
string result = value.ToString("P1");
Console.WriteLine(result);
medium
A. 75.7%
B. 0.8%
C. 0.76%
D. 75.67%

Solution

  1. Step 1: Understand "P1" format specifier

    "P" formats the number as a percentage by multiplying by 100 and adding "%". "1" means one decimal place.
  2. Step 2: Calculate output

    0.7567 * 100 = 75.67, rounded to one decimal place is 75.7, so output is "75.7%".
  3. Final Answer:

    75.7% -> Option A
  4. Quick Check:

    Percentage with 1 decimal = 75.7% [OK]
Hint: Percentage multiplies by 100 and adds % [OK]
Common Mistakes:
  • Not multiplying by 100 before formatting
  • Rounding incorrectly to 0.8%
  • Confusing decimal places with digits
4. The following code produces unexpected output. What is the problem?
DateTime dt = DateTime.Now;
string formatted = dt.ToString("MM-dd-yyyyy");
Console.WriteLine(formatted);
medium
A. Incorrect use of uppercase 'MM' for month
B. Missing escape characters for dashes
C. Too many 'y' characters in the format string
D. DateTime cannot be formatted with ToString

Solution

  1. Step 1: Check the format string for errors

    The format string uses "yyyyy" which causes the fifth 'y' to be treated as a literal 'y', producing unexpected output; year specifier should be 4 'y' characters for full year.
  2. Step 2: Validate other parts

    Dashes do not need escaping, "MM" is correct for month, and DateTime supports ToString formatting.
  3. Final Answer:

    Too many 'y' characters in the format string -> Option C
  4. Quick Check:

    Year format max 4 'y' characters [OK]
Hint: Year format uses max 4 'y' characters [OK]
Common Mistakes:
  • Thinking dashes need escaping
  • Believing ToString can't format DateTime
  • Confusing month format with day format
5. You have a list of decimal prices and want to display each price formatted as currency with two decimals and a comma as thousand separator. Which code snippet correctly formats and prints the list?
List<decimal> prices = new() { 1234.5m, 98765.432m, 12m };
hard
A. foreach(var p in prices) Console.WriteLine(p.ToString("C2"));
B. foreach(var p in prices) Console.WriteLine(p.ToString("N"));
C. foreach(var p in prices) Console.WriteLine(p.ToString("F"));
D. foreach(var p in prices) Console.WriteLine(p.ToString("D2"));

Solution

  1. Step 1: Understand required format

    We want currency format with two decimals and thousand separators. "C2" means currency with 2 decimals.
  2. Step 2: Check other format specifiers

    "N" is number with commas but no currency symbol, "F" is fixed-point without commas, "D2" is decimal integer with 2 digits (not for decimals).
  3. Final Answer:

    foreach(var p in prices) Console.WriteLine(p.ToString("C2")); -> Option A
  4. Quick Check:

    Currency with 2 decimals = "C2" [OK]
Hint: Use "C2" for currency with 2 decimals and commas [OK]
Common Mistakes:
  • Using "N" which lacks currency symbol
  • Using "F" which lacks thousand separators
  • Using "D2" which is for integers only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard