Skip to content

Commit 4f1ba96

Browse files
committed
Implement System.Decimal.Scale
Adds System.Decimal.Scale, a property that returns the scaling factor of the decimal. Closes #65074.
1 parent 4e75015 commit 4f1ba96

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public partial struct Decimal
1919

2020
internal bool IsNegative => _flags < 0;
2121

22-
internal int Scale => (byte)(_flags >> ScaleShift);
23-
2422
private ulong Low64 => _lo64;
2523

2624
private static ref DecCalc AsMutable(ref decimal d) => ref Unsafe.As<decimal, DecCalc>(ref d);

src/libraries/System.Private.CoreLib/src/System/Decimal.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ private Decimal(in decimal d, int flags)
326326
_flags = flags;
327327
}
328328

329+
/// <summary>
330+
/// Gets the scaling factor of the decimal, which is a number from 0 to 28 that represents the number of decimal digits.
331+
/// </summary>
332+
public byte Scale => (byte)(_flags >> ScaleShift);
333+
329334
// Returns the absolute value of the given Decimal. If d is
330335
// positive, the result is d. If d is negative, the result
331336
// is -d.

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,7 @@ public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, S
20852085
public Decimal(uint value) { throw null; }
20862086
[System.CLSCompliantAttribute(false)]
20872087
public Decimal(ulong value) { throw null; }
2088+
public byte Scale { get { throw null; } }
20882089
public static System.Decimal Add(System.Decimal d1, System.Decimal d2) { throw null; }
20892090
public static System.Decimal Ceiling(System.Decimal d) { throw null; }
20902091
public static int Compare(System.Decimal d1, System.Decimal d2) { throw null; }

src/libraries/System.Runtime/tests/System/DecimalTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,37 @@ public void Ctor_LargeScale_ThrowsArgumentOutOfRangeException()
257257
AssertExtensions.Throws<ArgumentOutOfRangeException>("scale", () => new Decimal(1, 2, 3, false, 29));
258258
}
259259

260+
public static IEnumerable<object[]> Scale_TestData()
261+
{
262+
yield return new object[] { 10m, 0 };
263+
yield return new object[] { 1m, 0 };
264+
yield return new object[] { -1m, 0 };
265+
yield return new object[] { 1.0m, 1 };
266+
yield return new object[] { -1.0m, 1 };
267+
yield return new object[] { 1.1m, 1 };
268+
yield return new object[] { 1.00m, 2 };
269+
yield return new object[] { 1.01m, 2 };
270+
yield return new object[] { 1.0000000000000000000000000000m, 28};
271+
}
272+
273+
[Theory]
274+
[MemberData(nameof(Scale_TestData))]
275+
public static void Scale(decimal value, byte expectedScale)
276+
{
277+
Assert.Equal(expectedScale, value.Scale);
278+
}
279+
280+
[Theory]
281+
[InlineData(new int[] { 1, 0, 0, 0 }, 0)] // 1
282+
[InlineData(new int[] { 10, 0, 0, 65536 }, 1)] // 1.0
283+
[InlineData(new int[] { 100, 0, 0, 131072 }, 2)] // 1.00
284+
[InlineData(new int[] { 268435456, 1042612833, 542101086, 1835008 }, 28)] // 1.0000000000000000000000000000
285+
[InlineData(new int[] { 10, 0, 0, -2147418112 }, 1)] // -1.0
286+
public static void ScaleFromBits(int[] bits, byte expectedScale)
287+
{
288+
Assert.Equal(expectedScale, new decimal(bits).Scale);
289+
}
290+
260291
public static IEnumerable<object[]> Add_Valid_TestData()
261292
{
262293
yield return new object[] { 1m, 1m, 2m };
@@ -968,7 +999,7 @@ public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatP
968999
Assert.Equal(0, result);
9691000
}
9701001
}
971-
1002+
9721003
public static IEnumerable<object[]> Remainder_Valid_TestData()
9731004
{
9741005
decimal NegativeZero = new decimal(0, 0, 0, true, 0);

0 commit comments

Comments
 (0)