Skip to content

Commit abb3240

Browse files
committed
Drop the Debug.Assert that rejects decimals like 10.0m [patch]
ParseRenderedFloat trims trailing zeros from the rendered text, so a decimal with more than one integer digit and only zeros after the point, such as 10.0m, 100.00m or -20.0m, becomes "10." with an empty fraction. The assert after the loop required an empty fraction to come with a single integer digit, which the loop does not guarantee, so Debug builds ended the process with "Unexpected format: 10." on valid input. Release builds compiled the assert out and returned the right value. The code after the assert already handles an empty fraction, so the assert goes. Fixes #112 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
1 parent 299fd76 commit abb3240

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎PreciseNumber.Test/PreciseNumberConversionTests.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ public void FromDecimalIsExact()
8484
AssertFromInAllModes(0.0000000000000000000000000001m, "0.0000000000000000000000000001");
8585
}
8686

87+
[TestMethod]
88+
public void FromDecimalWithOnlyZerosAfterThePoint()
89+
{
90+
// Trimming the zeros of "10.0" leaves "10." with an empty fraction, which is valid text for
91+
// ten, not a malformed rendering. This runs in Debug, where a failed assert ends the process.
92+
Assert.AreEqual(P("10"), 10.0m.ToPreciseNumber());
93+
Assert.AreEqual(P("100"), 100.00m.ToPreciseNumber());
94+
Assert.AreEqual(P("-20"), (-20.0m).ToPreciseNumber());
95+
AssertFromInAllModes(10.0m, "10");
96+
AssertFromInAllModes(-1200.000m, "-1200");
97+
}
98+
8799
[TestMethod]
88100
public void FromBinaryFloatingPointUsesDecimalText()
89101
{

‎PreciseNumber/PreciseNumber.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,6 @@ private static PreciseNumber ParseRenderedFloat(ReadOnlySpan<char> text)
667667
ReadOnlySpan<char> fractionalComponent = decimalIndex < 0 ? "0".AsSpan() : text[(decimalIndex + 1)..];
668668
exponentValue -= fractionalComponent.Length;
669669

670-
Debug.Assert(fractionalComponent.Length != 0 || integerComponent.TrimStart("-").Length == 1, $"Unexpected format: {text}");
671-
672670
int digitLength = integerComponent.Length + fractionalComponent.Length;
673671
char[]? rentedDigits = digitLength > MaxStackAllocChars ? ArrayPool<char>.Shared.Rent(digitLength) : null;
674672
Span<char> stackDigits = stackalloc char[MaxStackAllocChars];

0 commit comments

Comments
 (0)