Skip to content

Numeric format strings: retarget net10.0, add section separators, composite formatting and TryFormat - #2171

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/101006-format-strings
Open

Numeric format strings: retarget net10.0, add section separators, composite formatting and TryFormat#2171
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/101006-format-strings

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Supports the republish of Standard and Custom Numeric Format Strings.

Retarget and package lift

  • Both projects go from net8.0 to net10.0.
  • Microsoft.NET.Test.Sdk 17.6.0 to 18.9.0, xunit 2.4.2 to 2.9.3, xunit.runner.visualstudio 2.4.5 to 4.0.0, coverlet.collector 6.0.0 to 10.0.1.

New sample code, because the article gains two sections that had no runnable code

  • CustomFormatStrings.Accounting, .BlankZero and .EmptyThirdSection cover the semicolon section separator. EmptyThirdSection exists because the behaviour is counter-intuitive and worth pinning: "#,##0.00;(#,##0.00);" formats 0.0 as 0.00, since an empty section is ignored and the value falls back to the first one. Blanking zero needs an empty literal instead, which is what BlankZero shows.
  • StandardFormatStrings.Aligned and .AlignedInterpolated show the composite format item's alignment component, {0,12:N2} and {value,-12:N2}.
  • StandardFormatStrings.TryFormatFixedPoint writes into a Span<char> and returns false rather than allocating when the buffer is too small.
  • StandardFormatStrings.FixedPointPrecisionDecimal exists so the article's rounding correction has a test behind it. The article previously said F2 rounds with MidpointRounding.AwayFromZero, which is true for decimal and false for double: 1.005 formats as 1.00 as a double and 1.01 as a decimal. Both directions are now pinned.

Tidy

  • StandardFormatsStrings.cs renamed to StandardFormatStrings.cs to match the class it holds, and the matching test file likewise. Git tracks both as renames.
  • CustomFormatStringsTests.cs moved from StandardAndCustomNumericFormatStringsTests to StandardAndCustomFormatStringsTests, the namespace the rest of the test project already uses.

Verification (SDK 10.0.302)

dotnet build -c Release   ->  0 Warning(s), 0 Error(s)
dotnet test -c Release    ->  Failed: 0, Passed: 27, Skipped: 0, Total: 27

Previously 13 tests; the 14 added cases are the new methods plus the two TryFormat paths and the two rounding directions.

…posite formatting and TryFormat

- Retarget both projects from net8.0 to net10.0.
- Lift test packages: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3,
  xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.
- CustomFormatStrings: add Accounting, BlankZero and EmptyThirdSection so the
  semicolon section separator the article now teaches is runnable, including the
  counter-intuitive case where an empty third section is ignored.
- StandardFormatStrings: add FixedPointPrecisionDecimal, Aligned,
  AlignedInterpolated and TryFormatFixedPoint.
- Tests pin the rounding difference between double and decimal under "F2", the
  composite format item's alignment component, and both TryFormat paths
  (big enough buffer and too small a buffer).
- Tidy: StandardFormatsStrings.cs renamed to StandardFormatStrings.cs (and its
  test file likewise) to match the class name, and CustomFormatStringsTests
  moved to the same namespace as the rest of the test project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant