What's wrong
PreciseNumber implements IFormattable/ISpanFormattable but rejects every format string other than empty or "G".
Where
PreciseNumber/PreciseNumber.cs:1354-1358 (TryFormat, which ToString(format, provider) goes through):
if (!format.IsEmpty && !format.Equals("G", StringComparison.OrdinalIgnoreCase))
{
throw new FormatException();
}
Failure scenarios
Confirmed with a scratch app:
x.ToString("F2", CultureInfo.InvariantCulture) throws FormatException.
$"{x:0.00}" and string.Format("{0:N2}", x) throw.
- Generic
INumber<T> code that formats with a standard format string, logging templates such as {Value:N2}, and UI string-format bindings all fail at runtime, with a bare FormatException that has no message.
- Output is always fixed-point with no
E form, so a value like 1e-1000000 can only be rendered as a million-character string.
This is separate from #132, which is about NumberStyles in Parse.
Suggested fix
- Support at least:
F<n> and N<n>: Round(n) plus culture decimal and group separators from the provider.
E<n>: scientific notation, built from the significand and exponent.
R: same as G.
- For custom or unknown format strings, fall back to
G (or throw a FormatException with a message naming the supported formats) rather than throwing a bare one.
- Add tests for
F2, N2, E5, R, and an interpolated {x:F2}.
What's wrong
PreciseNumberimplementsIFormattable/ISpanFormattablebut rejects every format string other than empty or"G".Where
PreciseNumber/PreciseNumber.cs:1354-1358(TryFormat, whichToString(format, provider)goes through):Failure scenarios
Confirmed with a scratch app:
x.ToString("F2", CultureInfo.InvariantCulture)throwsFormatException.$"{x:0.00}"andstring.Format("{0:N2}", x)throw.INumber<T>code that formats with a standard format string, logging templates such as{Value:N2}, and UI string-format bindings all fail at runtime, with a bareFormatExceptionthat has no message.Eform, so a value like1e-1000000can only be rendered as a million-character string.This is separate from #132, which is about
NumberStylesinParse.Suggested fix
F<n>andN<n>:Round(n)plus culture decimal and group separators from the provider.E<n>: scientific notation, built from the significand and exponent.R: same asG.G(or throw aFormatExceptionwith a message naming the supported formats) rather than throwing a bare one.F2,N2,E5,R, and an interpolated{x:F2}.