Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update example of String Format instead of just round trip #790

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/Chapter02.Tests/Listing02.09.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AddisonWesley.Michaelis.EssentialCSharp.Shared;
using System.Globalization;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter02.Listing02_09.Tests;

Expand All @@ -8,19 +8,14 @@ public class ProgramTests
[TestMethod]
public void Main_WriteBooleanStatements()
{
string netCoreApp2expected =
$@"False: {1.61803398874989} == {1.61803398874989}
True: {1.61803398874989} == {1.61803398874989}";

string expected = // netcoreapp3.0 and later
$@"True: {1.618033988749895} == {1.618033988749895}
True: {1.618033988749895} == {1.618033988749895}";

string netCoreVersion = NetCore.GetNetCoreVersion();
if (string.Compare(netCoreVersion, "3")<0)
{
expected = netCoreApp2expected;
}
var number = 86540910.21;
string currentCultureString = number.ToString("C");
string greekCultureString = number.ToString("C", CultureInfo.GetCultureInfo("el-GR"));
string expected =
$@"{number}
{currentCultureString}
True: {currentCultureString} == {currentCultureString}
{greekCultureString}";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
Expand Down
24 changes: 0 additions & 24 deletions src/Chapter02/Listing02.09.FormattingUsingTheRFormatSpecifier.cs

This file was deleted.

32 changes: 32 additions & 0 deletions src/Chapter02/Listing02.09.UsingStringFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter02.Listing02_09;

using System;
using System.Globalization;

public class Program
{
public static void Main()
{
#region INCLUDE
// ...
const double number = 86540910.21;
string currencyText;

currencyText = $"{number}";
Console.WriteLine(currencyText);

currencyText = $"{number:C}";
Console.WriteLine(currencyText);

// Prove that string interpolation and the ToString method produce equivalent results with format specifiers
string toStringCurrencyText = number.ToString("C");
Console.WriteLine($"{currencyText == toStringCurrencyText}: {currencyText} == {toStringCurrencyText}");

// el-GR represents the Greek locale code
toStringCurrencyText = number.ToString("C", CultureInfo.GetCultureInfo("el-GR"));
BenjaminMichaelis marked this conversation as resolved.
Show resolved Hide resolved
Console.WriteLine(toStringCurrencyText);

// ...
#endregion INCLUDE
}
}
Loading