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

Decimal128.ToDecimal should not throw an exception when the conversion can succeed with a loss of precision

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.5
    • Affects Version/s: 2.4.4
    • Component/s: BSON
    • Labels:
      None
    • Environment:
      Win10

      The following causes an overflow error:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      var strValue = "10000.0000000000000000000000001";
      Decimal128 decimal128Value;
      Decimal128.TryParse( strValue, out decimal128Value );
      var decimalValue = Decimal128.ToDecimal( decimal128Value );
      

      Not that converting to double does not overflow:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      var doubleValue = Decimal128.ToDouble( decimal128Value );
      

      It is unclear why the Decimal128 value offers different precision than a standard .NET decimal. However the conversion to the .NET decimal should allow for a truncation of precision if necessary, as there currently is no other safe way to convert to decimal without going through some other type first (string or double).

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            ral628 Ryan Lawrence
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: