Announcement

Collapse
No announcement yet.

[CLOSED] [#409] [1.9] Decimal Rounding Bug

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [CLOSED] [#409] [1.9] Decimal Rounding Bug

    The default decimal rounding used by the Bridge.NET decimal implementation does not use the same default rounding as C#. I would expect bankers rounding to be used, so if Math.Round were called on 3.5 or 4.5 they would both produce the same result and round to 4.

    When running the following test code you can see that 3.5 rounds to 3 and 4.5 rounds to 4. The problem seems to be that the MidpointRounding enum is not being mapped to the rounding values used by the Decimal.js library so instead of using ROUND_HALF_EVEN as expected we're using ROUND_DOWN.

    Test Code:

    using System;
    using Bridge.Html5;
    
    namespace BridgeTest
    {
    
        public class App
        {
            [Ready]
            public static void Main()
            {
                decimal a = 3.5M;
                decimal b = 4.5M;
                Global.Alert(string.Format("Round up: {0}, Round Down: {1}", Math.Round(a), Math.Round(b)));
            }
        }
    }
    Expected Output:
    Round up: 4, Round Down: 4

    #2
    Nice find, and interesting problem.

    Definitely a defect.

    Reproduced with the following C# Console App.

    using System;
    
    namespace ConsoleApplication1
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                decimal a = 3.5M;
                decimal b = 4.5M;
                Console.WriteLine(string.Format("Round up: {0}, Round Down: {1}", Math.Round(a), Math.Round(b)));
                // Results:
                // Round up: 4, Round Down: 4
    
                Console.ReadLine();
            }
        }
    }

    Comment


      #3
      A new Issue has been created:

      https://github.com/bridgedotnet/Bridge/issues/409

      Comment


        #4
        Thanks Geoffrey.

        Comment


          #5
          The issue has been fixed. We will include the fix in 1.9 release.

          Comment


            #6
            Thanks guys!

            Comment


              #7
              Please note that new rounding modes were intoroduced
              https://github.com/bridgedotnet/Brid...intRounding.cs

              Comment


                #8
                It looks like there are still some issues with decimal rounding. The case of using Math.Round has been fixed, but I would expect the same results if decimal.Round was used instead. However, this is still using the old rounding method.

                Comment


                  #9
                  Yes, sorry for that. We will fix decimal.Round also

                  Comment


                    #10
                    Thanks Vladimir.

                    Comment


                      #11
                      GitHub issue:
                      https://github.com/bridgedotnet/Bridge/issues/442

                      Comment

                      Working...
                      X