Skip to content

Commit 8028516

Browse files
Youssef1313BillWagner
authored andcommitted
Correct code (#16747)
* Correct code * Update interpolated-strings.yml
1 parent 44d274c commit 8028516

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

docs/csharp/tutorials/exploration/interpolated-strings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ items:
4646
```csharp
4747
var item = (Name: "eggplant", Price: 1.99m, perPackage: 3);
4848
var date = DateTime.Now;
49-
Console.WriteLine($"On {date}, the price of {item.Name} was {item.Price:C2} per {item.perPackage} items.");
49+
Console.WriteLine($"On {date}, the price of {item.Name} was {item.Price} per {item.perPackage} items.");
5050
```
5151
5252
Note that the interpolation expression `item.Price` in the interpolated string resolves to the text "1.99" in the result string. That's because, when the type of the expression result is not a string, the result is resolved to a string in the following way:
@@ -66,7 +66,7 @@ items:
6666
In the previous step, two poorly formatted strings were inserted into the result string. One was a date and time value for which only the date was appropriate. The second was a price that didn't indicate its unit of currency. Both issues are easy to address. String interpolation lets you specify *format strings* that control the formatting of particular types. Modify the call to `Console.WriteLine` from the previous example to include the format strings for the date and price expressions as shown in the following line:
6767
6868
```csharp
69-
Console.WriteLine($"On {date:d}, the price of {item.Name} was {item.Price} per {item.perPackage} items");
69+
Console.WriteLine($"On {date:d}, the price of {item.Name} was {item.Price:C2} per {item.perPackage} items");
7070
```
7171
7272
You specify a format string by following the interpolation expression with a colon (":") and the format string. "d" is a [standard date and time format string](../../../standard/base-types/standard-date-and-time-format-strings.md#the-short-date-d-format-specifier) that represents the short date format. "C2" is a [standard numeric format string](../../../standard/base-types/standard-numeric-format-strings.md#the-currency-c-format-specifier) that represents a number as a currency value with two digits after the decimal point.

0 commit comments

Comments
 (0)