Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/MongoDB.Bson/ObjectModel/Decimal128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,14 @@ public static byte ToByte(Decimal128 d)
/// <returns>A <see cref="decimal"/> equivalent to <paramref name="d" />.</returns>
public static decimal ToDecimal(Decimal128 d)
{
if (d == Decimal128.MaxValue)
{
return decimal.MaxValue;
}
else if (d == Decimal128.MinValue)
{
return decimal.MinValue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this block into the IsFirstForm block?

if (Flags.IsFirstForm(d._highBits))
{
if (Decimal128.Compare(d, __minDecimalValue) < 0 || Decimal128.Compare(d, __maxDecimalValue) > 0)
Expand Down
2 changes: 2 additions & 0 deletions tests/MongoDB.Bson.Tests/ObjectModel/Decimal128Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void Decimal(string valueString, string expectedResult)
[InlineData("1E-99", "0")]
[InlineData("1E-6111", "0")]
[InlineData("10000.0000000000000000000000001", "10000.000000000000000000000000")] // see: CSHARP-2001
[InlineData("9999999999999999999999999999999999E+6111", "79228162514264337593543950335")] // see: CSHARP-5792
[InlineData("-9999999999999999999999999999999999E+6111", "-79228162514264337593543950335")]
public void ToDecimal_should_return_expected_result(string valueString, string expectedResultString)
{
var subject = Decimal128.Parse(valueString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public void TestConversions()
Assert.Equal((long)(ulong)int.MaxValue, converter.ToInt64(int.MaxValue));
Assert.Equal(1UL, converter.ToUInt64((long)1));
Assert.Equal((long)(ulong)long.MaxValue, converter.ToInt64(long.MaxValue));

// general decimal <-> Decimal128 checks
Assert.Equal(123.45m, converter.ToDecimal(new Decimal128(123.45m)));
Assert.Equal(-123.45m, converter.ToDecimal(new Decimal128(-123.45m)));
// System.Decimal should be mapped to Decimal128.MaxValue and vice versa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a blank line above this comment

Assert.Equal(Decimal128.MaxValue, converter.ToDecimal128(decimal.MaxValue));
Assert.Equal(Decimal128.MinValue, converter.ToDecimal128(decimal.MinValue));
Assert.Equal(decimal.MaxValue, converter.ToDecimal(Decimal128.MaxValue));
Assert.Equal(decimal.MinValue, converter.ToDecimal(Decimal128.MinValue));
}

[Theory]
Expand Down