diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Decimal.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Decimal.cs index b150aa78fcbe..93959816afdd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Decimal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Decimal.cs @@ -26,6 +26,8 @@ public static partial class Utf8Formatter /// System.FormatException if the format is not valid for this data type. /// public static bool TryFormat(decimal value, Span destination, out int bytesWritten, StandardFormat format = default) => - FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); + format.IsDefault + ? Number.TryFormatInvariantDecimalGeneral(value, destination, out bytesWritten) + : FormattingHelpers.TryFormat(value, destination, out bytesWritten, format); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs index fc6af82f7200..974ce82b7e0b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs @@ -553,6 +553,10 @@ public static unsafe string FormatDecimal(decimal value, ReadOnlySpan form Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte)); char fmt = ParseFormatSpecifier(format, out int digits); + if (digits == -1 && (fmt | 0x20) == 'g' && ReferenceEquals(info, NumberFormatInfo.InvariantInfo)) + { + return TryFormatInvariantDecimalGeneral(value, destination, out charsWritten); + } byte* pDigits = stackalloc byte[DecimalNumberBufferLength]; NumberBuffer number = new NumberBuffer(NumberBufferKind.Decimal, pDigits, DecimalNumberBufferLength); @@ -576,6 +580,97 @@ public static unsafe string FormatDecimal(decimal value, ReadOnlySpan form return success; } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static unsafe bool TryFormatInvariantDecimalGeneral(decimal value, Span destination, out int charsWritten) + where TChar : unmanaged, IUtfChar + { + int scale = value.Scale; + if (value.High == 0 && scale is 2 or 4) + { + return TryFormatInvariantDecimalSmallScale(value.Low64, decimal.IsNegative(value), scale, destination, out charsWritten); + } + + UInt128 coefficient = new(value.High, value.Low64); + int integralDigits = FormattingHelpers.CountDigits(coefficient) - scale; + int signLength = decimal.IsNegative(value) && coefficient != 0 ? 1 : 0; + int length = signLength + Math.Max(integralDigits, 1) + (scale == 0 ? 0 : scale + 1); + + if (length > destination.Length) + { + charsWritten = 0; + return false; + } + + if (signLength != 0) + { + destination[0] = TChar.CastFrom('-'); + } + + fixed (TChar* buffer = &MemoryMarshal.GetReference(destination)) + { + UInt128ToDecChars(buffer + length, coefficient); + } + + if (scale != 0) + { + if (integralDigits <= 0) + { + destination[signLength] = TChar.CastFrom('0'); + destination[signLength + 1] = TChar.CastFrom('.'); + destination.Slice(signLength + 2, -integralDigits).Fill(TChar.CastFrom('0')); + } + else + { + // Move the integral digits left one place to open the decimal point. + destination.Slice(signLength + 1, integralDigits).CopyTo(destination.Slice(signLength)); + destination[signLength + integralDigits] = TChar.CastFrom('.'); + } + } + + charsWritten = length; + return true; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe bool TryFormatInvariantDecimalSmallScale(ulong coefficient, bool negative, int scale, Span destination, out int charsWritten) + where TChar : unmanaged, IUtfChar + { + (ulong whole, ulong fraction) = scale == 2 + ? Math.DivRem(coefficient, 100ul) + : Math.DivRem(coefficient, 10_000ul); + int signLength = negative && coefficient != 0 ? 1 : 0; + int pointIndex = signLength + FormattingHelpers.CountDigits(whole); + int length = pointIndex + 1 + scale; + if (length > destination.Length) + { + charsWritten = 0; + return false; + } + + fixed (TChar* buffer = &MemoryMarshal.GetReference(destination)) + { + if (signLength != 0) + { + buffer[0] = TChar.CastFrom('-'); + } + + TChar* point = buffer + pointIndex; + UInt64ToDecChars(point, whole); + *point = TChar.CastFrom('.'); + if (scale == 2) + { + WriteTwoDigits((uint)fraction, point + 1); + } + else + { + WriteFourDigits((uint)fraction, point + 1); + } + } + + charsWritten = length; + return true; + } + internal static void DecimalIeee754ToNumber(TValue value, ref NumberBuffer number) where TDecimal : unmanaged, IDecimalIeee754ParseAndFormatInfo where TValue : unmanaged, IBinaryInteger diff --git a/src/libraries/System.Memory/tests/ParsersAndFormatters/TestData.cs b/src/libraries/System.Memory/tests/ParsersAndFormatters/TestData.cs --- a/src/libraries/System.Memory/tests/ParsersAndFormatters/TestData.cs +++ b/src/libraries/System.Memory/tests/ParsersAndFormatters/TestData.cs @@ -238,6 +238,13 @@ yield return -999.99m; yield return 0.000123456m; yield return -0.000123456m; + + // Scale two on either side of the 64-bit coefficient boundary. + yield return new decimal(-1, -1, 0, isNegative: false, 2); + yield return new decimal(0, 0, 1, isNegative: false, 2); + + yield return -0.0030m; + yield return new decimal(0, 0, 0, isNegative: true, 4); // Explicit trailing 0's (Decimal can and does preserve these by setting the Scale appropriately) yield return 1.00m; # End of patch