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 155 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 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
                    • 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

                      B Offline
                      B Offline
                      Bosse62
                      wrote on last edited by
                      #21

                      I learned in school: "When you have a 5 to round from; the number to round to shall be even." Let me give you some examples: Rounding to no decimals: 0.5 --> 0 1.5 --> 2 2.5 --> 2 3.5 --> 4 Rounding to one decimal: 0.35 --> 0.4 0.45 --> 0.4 0.55 --> 0.6 0.65 --> 0.6

                      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