Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public static class CustomFormatStrings
public static string DigitSeparator(double number) => number.ToString("#,###.00");
public static string Phone(long value) => string.Format("{0:(###) ###-####}", value);
public static string PhoneInterpolated(long value) => $"{value:(###) ###-####}";
public static string Accounting(double number) => number.ToString("#,##0.00;(#,##0.00);Zero");
public static string BlankZero(double number) => number.ToString("0;;''");
public static string EmptyThirdSection(double number) => number.ToString("#,##0.00;(#,##0.00);");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public static class StandardFormatStrings
public static string DecimalPrecision(int value) => value.ToString("D5");
public static string FloatingPointPrecision(double value) => value.ToString("F2");
public static string Percentage(double value) => $"{value:P2}";
public static string FixedPointPrecisionDecimal(decimal value) => value.ToString("F2");
public static string Aligned(double value) => string.Format("{0,12:N2}", value);
public static string AlignedInterpolated(double value) => $"{value,-12:N2}";

public static bool TryFormatFixedPoint(double value, Span<char> destination, out int charsWritten) =>
value.TryFormat(destination, out charsWritten, "F2", CultureInfo.InvariantCulture);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace StandardAndCustomNumericFormatStringsTests;
namespace StandardAndCustomFormatStringsTests;

public class CustomFormatStringsTests
{
Expand Down Expand Up @@ -56,4 +56,35 @@ public void GivenANumber_WhenPhoneInterpolated_ReturnsFormattedPhoneNumber(long

Assert.Equal(expected, result);
}
}

[Theory]
[InlineData(1234.5, "1,234.50")]
[InlineData(-1234.5, "(1,234.50)")]
[InlineData(0.0, "Zero")]
public void GivenAValue_WhenAccounting_ThenTheSectionMatchingTheSignIsUsed(double input, string expected)
{
string result = CustomFormatStrings.Accounting(input);

Assert.Equal(expected, result);
}

[Theory]
[InlineData(1234.5, "1235")]
[InlineData(-1234.5, "-1235")]
[InlineData(0.0, "")]
public void GivenAValue_WhenBlankZero_ThenZeroFormatsAsAnEmptyString(double input, string expected)
{
string result = CustomFormatStrings.BlankZero(input);

Assert.Equal(expected, result);
}

[Theory]
[InlineData(0.0, "0.00")]
public void GivenZero_WhenTheThirdSectionIsEmpty_ThenItIsIgnoredAndTheFirstSectionIsUsed(double input, string expected)
{
string result = CustomFormatStrings.EmptyThirdSection(input);

Assert.Equal(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,62 @@ public void GivenANumber_WhenPercentage_ReturnsPercentage(double input, string e

Assert.Contains(expected, result);
}
}

[Theory]
[InlineData(1.005, "1.00")]
[InlineData(0.125, "0.12")]
public void GivenAnApparentMidpoint_WhenFloatingPointPrecision_ThenTheStoredBinaryValueDecides(double input, string expected)
{
string result = StandardFormatStrings.FloatingPointPrecision(input);

Assert.Equal(expected, result);
}

[Fact]
public void GivenAMidpoint_WhenFixedPointPrecisionDecimal_ThenItRoundsAwayFromZero()
{
Assert.Equal("1.01", StandardFormatStrings.FixedPointPrecisionDecimal(1.005m));
Assert.Equal("0.13", StandardFormatStrings.FixedPointPrecisionDecimal(0.125m));
}

[Theory]
[InlineData(1652.5899, " 1,652.59")]
public void GivenAValue_WhenAligned_ThenItIsRightAlignedInATwelveCharacterField(double input, string expected)
{
string result = StandardFormatStrings.Aligned(input);

Assert.Equal(expected, result);
}

[Theory]
[InlineData(1652.5899, "1,652.59 ")]
public void GivenAValue_WhenAlignedInterpolated_ThenANegativeWidthLeftAlignsIt(double input, string expected)
{
string result = StandardFormatStrings.AlignedInterpolated(input);

Assert.Equal(expected, result);
}

[Fact]
public void GivenABigEnoughBuffer_WhenTryFormatFixedPoint_ThenItWritesTheValueAndReturnsTrue()
{
Span<char> buffer = stackalloc char[16];

bool succeeded = StandardFormatStrings.TryFormatFixedPoint(1652.5899, buffer, out int charsWritten);

Assert.True(succeeded);
Assert.Equal(7, charsWritten);
Assert.Equal("1652.59", buffer[..charsWritten].ToString());
}

[Fact]
public void GivenATooSmallBuffer_WhenTryFormatFixedPoint_ThenItWritesNothingAndReturnsFalse()
{
Span<char> buffer = stackalloc char[2];

bool succeeded = StandardFormatStrings.TryFormatFixedPoint(1652.5899, buffer, out int charsWritten);

Assert.False(succeeded);
Assert.Equal(0, charsWritten);
}
}
Loading