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

Decimal128 to decimal conversion loses precision for zero

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 2.8.0
    • Fix Version/s: 2.8.1
    • Component/s: Bson
    • Labels:
      None
    • Environment:
      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

            Assignee:
            dmitry.lukyanov Dmitry Lukyanov
            Reporter:
            tmh@fundconnect.com Thomas Hjorslev
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: