0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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.