From abb324080579231c74adef4205cd891ca601bcf6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 21:26:35 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN --- PreciseNumber.Test/PreciseNumberConversionTests.cs | 12 ++++++++++++ PreciseNumber/PreciseNumber.cs | 2 -- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/PreciseNumber.Test/PreciseNumberConversionTests.cs b/PreciseNumber.Test/PreciseNumberConversionTests.cs index bb69740..e0ef1da 100644 --- a/PreciseNumber.Test/PreciseNumberConversionTests.cs +++ b/PreciseNumber.Test/PreciseNumberConversionTests.cs @@ -84,6 +84,18 @@ public void FromDecimalIsExact() AssertFromInAllModes(0.0000000000000000000000000001m, "0.0000000000000000000000000001"); } + [TestMethod] + public void FromDecimalWithOnlyZerosAfterThePoint() + { + // Trimming the zeros of "10.0" leaves "10." with an empty fraction, which is valid text for + // ten, not a malformed rendering. This runs in Debug, where a failed assert ends the process. + Assert.AreEqual(P("10"), 10.0m.ToPreciseNumber()); + Assert.AreEqual(P("100"), 100.00m.ToPreciseNumber()); + Assert.AreEqual(P("-20"), (-20.0m).ToPreciseNumber()); + AssertFromInAllModes(10.0m, "10"); + AssertFromInAllModes(-1200.000m, "-1200"); + } + [TestMethod] public void FromBinaryFloatingPointUsesDecimalText() { diff --git a/PreciseNumber/PreciseNumber.cs b/PreciseNumber/PreciseNumber.cs index fc17624..7dd2164 100644 --- a/PreciseNumber/PreciseNumber.cs +++ b/PreciseNumber/PreciseNumber.cs @@ -667,8 +667,6 @@ private static PreciseNumber ParseRenderedFloat(ReadOnlySpan text) ReadOnlySpan fractionalComponent = decimalIndex < 0 ? "0".AsSpan() : text[(decimalIndex + 1)..]; exponentValue -= fractionalComponent.Length; - Debug.Assert(fractionalComponent.Length != 0 || integerComponent.TrimStart("-").Length == 1, $"Unexpected format: {text}"); - int digitLength = integerComponent.Length + fractionalComponent.Length; char[]? rentedDigits = digitLength > MaxStackAllocChars ? ArrayPool.Shared.Rent(digitLength) : null; Span stackDigits = stackalloc char[MaxStackAllocChars];