Skip to content

Commit 864b999

Browse files
committed
Fix reading numerics with big scale difference (#5851)
Fixes #5848 (cherry picked from commit 9ca526b)
1 parent ae45245 commit 864b999

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/Npgsql/Internal/Converters/Primitive/PgNumeric.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,24 @@ internal static decimal ToDecimal(short scale, short weight, ushort sign, Span<s
380380
result += digit;
381381

382382
if (scaleDifference < 0)
383-
result /= UIntPowers10[-scaleDifference];
383+
{
384+
// Doesn't look like we can loop even once, but just to be on a safe side
385+
while (scaleDifference < 0)
386+
{
387+
var scaleChunk = Math.Min(MaxUIntScale, -scaleDifference);
388+
result /= UIntPowers10[scaleChunk];
389+
scaleDifference += scaleChunk;
390+
}
391+
}
384392
else
393+
{
385394
while (scaleDifference > 0)
386395
{
387396
var scaleChunk = Math.Min(MaxUIntScale, scaleDifference);
388-
result *= UIntPowers10[scaleChunk];
397+
scaleFactor *= UIntPowers10[scaleChunk];
389398
scaleDifference -= scaleChunk;
390399
}
400+
}
391401
}
392402

393403
result *= scaleFactor;

test/Npgsql.Tests/Types/NumericTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public class NumericTests : MultiplexingTestBase
7777

7878
// Bug 2033
7979
new object[] { "0.0036882500000000000000000000", 0.0036882500000000000000000000M },
80+
// Bug 5848
81+
new object[] { "10836968.715000000000000000000000", 10836968.715000000000000000000000M },
8082

8183
new object[] { "936490726837837729197", 936490726837837729197M },
8284
new object[] { "9364907268378377291970000", 9364907268378377291970000M },

0 commit comments

Comments
 (0)