Repository navigation
Support the F, N, E, R and G<n> standard format strings [patch] - #170
Merged
Merged
Conversation
ToString and TryFormat threw a bare FormatException for every format
other than empty or "G", so $"{x:F2}", "N2" and logging templates
crashed at runtime.
Fixed point (F), number (N, with the culture's group sizes, separator
and negative pattern) and scientific (E) now render from the exact
digits, rounding half away from zero like Round. R is the exact general
form, and G<n> reduces to n significant digits first. Any other format,
including custom ones such as "0.00", throws a FormatException whose
message names the supported formats.
Fixes #134
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BPNa479nM4FXr2G22kBHmy
Addresses the SonarCloud findings on this change: FormatFixedPoint's cognitive complexity (integral digits and the negative pattern now live in their own helpers) and MSTEST0046 in the new tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BPNa479nM4FXr2G22kBHmy
|
This was referenced Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #134
Problem
TryFormat, which everyToString(format, provider)goes through, threw a bareFormatExceptionfor any format other than empty orG. So$"{x:F2}",string.Format("{0:N2}", x), logging templates, and genericINumber<T>code that formats values all crashed at runtime.Change
The new
PreciseNumber/PreciseNumber.Formatting.csrenders the standard formats from the exact significand and exponent. Nothing goes throughdouble, and rounding is half away from zero, the same asRound.1234.5678)F<n>Round(n), padded to exactlynfractional digits. Defaultnis the culture'sNumberDecimalDigitsF2→1234.57N<n>F, plus the culture's group sizes, group separator, andNumberNegativePatternN2→1,234.57E<n>nfractional digits, a signed exponent of at least 3 digits. Defaultnis 6, andegives a lowercase markerE5→1.23457E+003RG1234.5678G<n>ReduceSignificance(n), then the general form.G0keeps every digitG5→1234.60.00or#,##0,C,X,Rwith a precision, and precisions above 999,999,999 throw aFormatException. Its message names the format and lists the supported ones. The issue allowed either this or a fallback toG. I chose the exception because a silentGfallback would hand back unrounded digits to someone who asked for two decimals.Ewrites1.00E-1000000directly, with no million-character fixed-point expansion.TryFormatreturnsfalsewhen the destination is too small for a standard format.ToStringtakes these formats straight to the renderer, so it never comes back empty because its buffer guess was too small.OverflowException, as the existingGpath does.README.mddocuments the supported formats.Tests
PreciseNumberTestsgets new and updated cases:TestToStringStandardFormatshas 25 data rows coveringF/N/E/R/G<n>, default precisions, carries (999.995→1,000.00,9.9999→1.00E+001), zero, small negatives, and lowercasee.{x:F2}andstring.Format("{0:N2}").[3, 2], and negative pattern 0. These use a clonedNumberFormatInfo, because the test project runs in invariant-globalization mode.E2at an exponent of -1,000,000.TryFormatreturningfalsewhen the destination is too small.FormatExceptions for unsupported formats.TestToStringVariousFormatsandTestTryFormatVariousFormatspreviously asserted the old throw. They now assert the rendered text.Verification:
🤖 Generated with Claude Code
https://claude.ai/code/session_01BPNa479nM4FXr2G22kBHmy
Generated by Claude Code