📖 Scenario: You are creating a simple program to display prices and dates in a clear and friendly way for a small shop.
🎯 Goal: Learn how to use format specifiers in C# to show numbers as currency and dates in a readable format.
📋 What You'll Learn
Create variables for price and date with exact values
Add a format specifier variable for currency
Format the price using the currency format specifier
Format the date using a date format specifier
Print the formatted price and date
💡 Why This Matters
🌍 Real World
Formatting prices and dates is common in shopping apps, invoices, and reports to make information clear and professional.
💼 Career
Knowing how to format numbers and dates is essential for software developers working on user interfaces, financial software, and data presentation.
Progress0 / 4 steps
1
Create price and date variables
Create a decimal variable called price and set it to 1234.56m. Create a DateTime variable called saleDate and set it to new DateTime(2024, 6, 15).
C Sharp (C#)
Hint
Use decimal for money values and DateTime for dates.
2
Add currency format specifier
Create a string variable called currencyFormat and set it to "C" to format numbers as currency.
C Sharp (C#)
Hint
The "C" format specifier shows numbers as currency.
3
Format price and date
Create a string variable called formattedPrice and set it to price.ToString(currencyFormat). Create a string variable called formattedDate and set it to saleDate.ToString("MMMM dd, yyyy") to show the full month name, day, and year.
C Sharp (C#)
Hint
Use ToString with format specifiers to convert numbers and dates to strings.
4
Print formatted price and date
Write two Console.WriteLine statements to print formattedPrice and formattedDate on separate lines.
C Sharp (C#)
Hint
Use Console.WriteLine to show the results on the screen.
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
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.
Step 2: Compare other specifiers
"N" formats as number with commas, "P" as percentage, "D" as decimal integer. Only "C" fits currency.
Final Answer:
"C" -> Option B
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
Step 1: Identify correct year format specifier
In C#, "yyyy" outputs the full four-digit year, while "yy" outputs two digits.
Step 2: Check case sensitivity and keywords
"YYYY" is invalid (case sensitive), and "Year" is not a recognized format string.
Final Answer:
date.ToString("yyyy") -> Option D
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
Step 1: Understand "P1" format specifier
"P" formats the number as a percentage by multiplying by 100 and adding "%". "1" means one decimal place.
Step 2: Calculate output
0.7567 * 100 = 75.67, rounded to one decimal place is 75.7, so output is "75.7%".
Final Answer:
75.7% -> Option A
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?
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.
Step 2: Validate other parts
Dashes do not need escaping, "MM" is correct for month, and DateTime supports ToString formatting.
Final Answer:
Too many 'y' characters in the format string -> Option C
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?