Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2582

Decimal128 to decimal conversion loses precision for zero

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 2.8.1
    • 2.8.0
    • BSON
    • None
    • Windows 10, dotnet core 2.1

    Description

      When converting a decimal to Decimal128 and back, the result is not as expected when the original decimal is "zero with decimals", e.g. 0.000

      Code to reproduce:

      var originals = new[] { 1M, 1.000M, 0M, 0.000M };
      foreach( var original in originals )
      {
          var dec128 = new Decimal128( original );
          var newDecimal = Decimal128.ToDecimal( dec128 );
          if( !decimal.GetBits( original ).SequenceEqual( decimal.GetBits( newDecimal ) ) )
              Console.Write( "!" );
          Console.WriteLine( $"{original} = {dec128} = {newDecimal}" );
      }
      

      The last value, 0.000M is converted to a straight Decimal.Zero.

      I think this is due to line 719 in Decimal128.cs:

       if (Decimal128.IsZero(d))
      {
          return decimal.Zero;
      }
      

      I don't believe this is the correct behavior.

      Attachments

        Activity

          People

            dmitry.lukyanov@mongodb.com Dmitry Lukyanov (Inactive)
            tmh@fundconnect.com Thomas Hjorslev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: