Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Should 0.5 round up or down?

Should 0.5 round up or down?

Scheduled Pinned Locked Moved C#
question
21 Posts 7 Posters 183 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1
         float n = 0.2f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
         
         n = 0.5f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 0.6f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.5f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.2f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    
         n = 1.7f;
         Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
    

    Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

    "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

    T D K J L 6 Replies Last reply
    0
    • L Lost User
           float n = 0.2f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
           
           n = 0.5f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
      
           n = 0.6f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
      
           n = 1.5f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
      
           n = 1.2f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
      
           n = 1.7f;
           Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
      

      Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      T Offline
      T Offline
      trønderen
      wrote on last edited by
      #2

      Take a look at IEEE 754 rounding. Wikipedia (IEEE 754 Rounding rules[^]) is a good starting point, but not necessarily the ultimate answer. For any calculation with bits beyond what can be represented in the result definition, as long as they are available they should determine rounding. However, if the true result of a calculation is exactly one half of the resolution (so you do not have any hidden bits to help you), experts on error propagation will tell you that rounding up or down at random, or e.g. every second time, will reduce the average error (when we are talking about zillions of calculations and result roundings).

      Religious freedom is the freedom to say that two plus two make five.

      1 Reply Last reply
      0
      • L Lost User
             float n = 0.2f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
             
             n = 0.5f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
        
             n = 0.6f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
        
             n = 1.5f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
        
             n = 1.2f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
        
             n = 1.7f;
             Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
        

        Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        As always, it depends. The Round function can use different rules depending on what you pass it. It's all covered in the documentation on Round[^]. Just make sure you have the correct framework you're using selected in the top left of the page.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

        1 Reply Last reply
        0
        • L Lost User
               float n = 0.2f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
               
               n = 0.5f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
          
               n = 0.6f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
          
               n = 1.5f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
          
               n = 1.2f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
          
               n = 1.7f;
               Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
          

          Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          K Offline
          K Offline
          Kenneth Haugland
          wrote on last edited by
          #4

          It does both, does it not? 0.5 => 0 1.5 => 2 2.5 => 2 3.5 => 4 etc. This was mainly Carl Friedrich Gauss's idea. Basically, its a choice you make, and you could choose differently. Your computer might do it differently...

          1 Reply Last reply
          0
          • L Lost User
                 float n = 0.2f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                 
                 n = 0.5f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
            
                 n = 0.6f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
            
                 n = 1.5f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
            
                 n = 1.2f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
            
                 n = 1.7f;
                 Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
            

            Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            The question is not how to round. The question is what exact business rules are being addressed. This is much more true when dealing with currency of any sort. Often (always?) with currency rounding is covered by laws, regulations and/or contracts. And in complex systems one must do a data dip to determine the exact rule that applies for that case (always with a timestamp.)

            L 1 Reply Last reply
            0
            • J jschell

              The question is not how to round. The question is what exact business rules are being addressed. This is much more true when dealing with currency of any sort. Often (always?) with currency rounding is covered by laws, regulations and/or contracts. And in complex systems one must do a data dip to determine the exact rule that applies for that case (always with a timestamp.)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I did not ask "how" ... I learned to add 0.5 or 0.05 or .005 in elementary school. "Should" implies Generally Accepted Accounting Practices (GAAP) ... or what every search generally says about "rounding". I don't expect that every person who learned these "rules" goes back to check every day to see if "convention" has changed.

              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

              J 1 Reply Last reply
              0
              • L Lost User

                I did not ask "how" ... I learned to add 0.5 or 0.05 or .005 in elementary school. "Should" implies Generally Accepted Accounting Practices (GAAP) ... or what every search generally says about "rounding". I don't expect that every person who learned these "rules" goes back to check every day to see if "convention" has changed.

                "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                Not sure what your original post was referring to. But my point is that business rules, not code usage, determines how rounding should happen. So no one should ever code rounding without understanding the business rules that require it.

                L 1 Reply Last reply
                0
                • L Lost User
                       float n = 0.2f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                       
                       n = 0.5f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                  
                       n = 0.6f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                  
                       n = 1.5f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                  
                       n = 1.2f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                  
                       n = 1.7f;
                       Console.WriteLine( $"{n:F2}: {Math.Round( n, 0 ):F2}" );
                  

                  Answer: 0.20: 0.00 0.50: 0.00 0.60: 1.00 1.50: 2.00 1.20: 1.00 1.70: 2.00

                  "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  MidpointRounding Enum (System) | Microsoft Learn[^]

                  L 1 Reply Last reply
                  0
                  • J jschell

                    Not sure what your original post was referring to. But my point is that business rules, not code usage, determines how rounding should happen. So no one should ever code rounding without understanding the business rules that require it.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I think you just want to add your 2 cents regardless of whether you understand the "original post" or not.

                    "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                    1 Reply Last reply
                    0
                    • L Lost User

                      MidpointRounding Enum (System) | Microsoft Learn[^]

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      My "question" was more philosophical than technical. I have built ERP systems from the ground up that passed audit from national accounting firms. My "naive" approach to rounding was never a problem. (And this was "before" .NET) The concept of randomly deciding to go up or down, is not "determinate" from an auditing point of view.

                      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        My "question" was more philosophical than technical. I have built ERP systems from the ground up that passed audit from national accounting firms. My "naive" approach to rounding was never a problem. (And this was "before" .NET) The concept of randomly deciding to go up or down, is not "determinate" from an auditing point of view.

                        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        There is a good option there which is not random: always round to the even number. Not being an accountant I don't know if auditors have their own rule.

                        J L 2 Replies Last reply
                        0
                        • L Lost User

                          There is a good option there which is not random: always round to the even number. Not being an accountant I don't know if auditors have their own rule.

                          J Offline
                          J Offline
                          jschell
                          wrote on last edited by
                          #12

                          I have worked at different fintech companies for 10 years. At least in one part of the industry rounding was specifically regulated. It varied by locality. And over time.

                          L 1 Reply Last reply
                          0
                          • J jschell

                            I have worked at different fintech companies for 10 years. At least in one part of the industry rounding was specifically regulated. It varied by locality. And over time.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Yes but that is accounting, not C#/.NET.

                            T J 2 Replies Last reply
                            0
                            • L Lost User

                              There is a good option there which is not random: always round to the even number. Not being an accountant I don't know if auditors have their own rule.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              The "drift" on the internet is: 0.5 rounds to 1; not 0. Having assumed this all along, I was wondering if others had "contemplated" this; as opposed to simply saying "business rules". I usually talk "businesses" out of bad habits for the good of all (like deleting "code table" entries).

                              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                              1 Reply Last reply
                              0
                              • L Lost User

                                Yes but that is accounting, not C#/.NET.

                                T Offline
                                T Offline
                                trønderen
                                wrote on last edited by
                                #15

                                So what about a programming developing an accounting application in C#?

                                Religious freedom is the freedom to say that two plus two make five.

                                Richard DeemingR L 2 Replies Last reply
                                0
                                • T trønderen

                                  So what about a programming developing an accounting application in C#?

                                  Religious freedom is the freedom to say that two plus two make five.

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #16

                                  What would you do in any other language? There isn't a magic-bullet solution to this problem in any language. If the rules are subject to change, then you either need to recompile, or have some means of configuring the code to use specific rounding rules in specific situations. The only complaint with .NET / C# is if you're stuck with .NET Framework, where you only options are "to even" or "away from zero"; the options for "to zero", "to negative infinity", and "to positive infinity" were added in .NET Core 3.0.


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  J 1 Reply Last reply
                                  0
                                  • T trønderen

                                    So what about a programming developing an accounting application in C#?

                                    Religious freedom is the freedom to say that two plus two make five.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    As with all programming development, the code should implement the business rules.

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      Yes but that is accounting, not C#/.NET.

                                      J Offline
                                      J Offline
                                      jschell
                                      wrote on last edited by
                                      #18

                                      Not sure what you mean. I presume there are accountants that still use paper ledgers. But the vast majority of accountants use software applications. Consider any sort of interest bearing account. Savings, CDs, Loans, Mortgages, etc. The interest is calculated on a schedule that is either defined by the institution or some other regulatory body. That happens in software (vast majority of cases.)

                                      L 1 Reply Last reply
                                      0
                                      • J jschell

                                        Not sure what you mean. I presume there are accountants that still use paper ledgers. But the vast majority of accountants use software applications. Consider any sort of interest bearing account. Savings, CDs, Loans, Mortgages, etc. The interest is calculated on a schedule that is either defined by the institution or some other regulatory body. That happens in software (vast majority of cases.)

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        I merely pointed out to Gerry that .NET offers some options as to how numbers may be rounded, including the one to always round towards the even number. Whether he (or you) use that feature is a completely free choice, and has nothing to do with rules set by accounting, audit, or other business systems.

                                        1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          What would you do in any other language? There isn't a magic-bullet solution to this problem in any language. If the rules are subject to change, then you either need to recompile, or have some means of configuring the code to use specific rounding rules in specific situations. The only complaint with .NET / C# is if you're stuck with .NET Framework, where you only options are "to even" or "away from zero"; the options for "to zero", "to negative infinity", and "to positive infinity" were added in .NET Core 3.0.


                                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #20

                                          Richard Deeming wrote:

                                          is if you're stuck with .NET Framework,

                                          You can of course add code to potentially handle other situations. Probably the most stringent part of this is that the developer should be able to show why they are using a specific rounding and then document it. Probably need to explain it as well. Both for potential audits as well as insuring future maintenance programmers do not 'fix' anything. I have certainly had to explain this to non-developers before that should have already understood it since they were dealing with financial decisions daily. This would always be the case when dealing with financial transactions. This can even become a problem when developers decide to store currency amounts in floating point data types. Then even something like addition can be impacted by rounding. I never experienced a problem with scientific results but when I was programming for that I was not aware of any of the issues. To be fair back then even financial institutions did not seem aware of it. The theory was known but in had not made a transition to practical usage (in many cases.)

                                          Richard Deeming wrote:

                                          If the rules are subject to change, then you either need to recompile,

                                          It probably is going to be more complicated than that. For one time calculations then ok. But one must insure that that business rule will never change. The case I encountered was where the company was using paper records printed years before. Sometimes those were lost. So they printed (ran a report) again. After the rules had changed. No one in the company was aware of this problem until I pointed it out once I needed to do an update on the report. As you pointed out this required a dynamic solution which I implemented.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups