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. Other Discussions
  3. The Weird and The Wonderful
  4. MidPointRounding for Math.Round() method in .Net Application

MidPointRounding for Math.Round() method in .Net Application

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpphpcomhelptutorial
8 Posts 7 Posters 1 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.
  • E Offline
    E Offline
    elizas
    wrote on last edited by
    #1

    We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value. For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of Math.Round() method. Math.Round can also take a parameter known as MidPointRounding which helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero. For example, when someone do the rounding of 8.5 . MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9). http://www.mindfiresolutions.com/MidPointRounding-for-MathRound-method-in-Net-Application-293.php[^]

    Cheers, Eliza

    K P D P 4 Replies Last reply
    0
    • E elizas

      We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value. For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of Math.Round() method. Math.Round can also take a parameter known as MidPointRounding which helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero. For example, when someone do the rounding of 8.5 . MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9). http://www.mindfiresolutions.com/MidPointRounding-for-MathRound-method-in-Net-Application-293.php[^]

      Cheers, Eliza

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Wrong forum, this forum is for code that is soo ugly, its own momma vomits when she looks at it. Your post would be better in "Tips and Tricks".

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      1 Reply Last reply
      0
      • E elizas

        We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value. For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of Math.Round() method. Math.Round can also take a parameter known as MidPointRounding which helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero. For example, when someone do the rounding of 8.5 . MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9). http://www.mindfiresolutions.com/MidPointRounding-for-MathRound-method-in-Net-Application-293.php[^]

        Cheers, Eliza

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Or make it a blog entry.

        1 Reply Last reply
        0
        • E elizas

          We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value. For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of Math.Round() method. Math.Round can also take a parameter known as MidPointRounding which helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero. For example, when someone do the rounding of 8.5 . MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9). http://www.mindfiresolutions.com/MidPointRounding-for-MathRound-method-in-Net-Application-293.php[^]

          Cheers, Eliza

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          Not really relevant to this forum, but it has caught my attention. Are these really the only two options for rounding in .NET? What if I want (for example) to always round up (i.e. 1.5 becomes 2, but -1.5 becomes -1). That sounds a bit odd at first, but in some banking applications I have worked on that was how it worked because you rounded in the bank's favour. (Banks never miss a trick when it comes to money.)

          F J 2 Replies Last reply
          0
          • D David Skelly

            Not really relevant to this forum, but it has caught my attention. Are these really the only two options for rounding in .NET? What if I want (for example) to always round up (i.e. 1.5 becomes 2, but -1.5 becomes -1). That sounds a bit odd at first, but in some banking applications I have worked on that was how it worked because you rounded in the bank's favour. (Banks never miss a trick when it comes to money.)

            F Offline
            F Offline
            fabrice leal
            wrote on last edited by
            #5

            Use the Math.Ceiling(1.5) = 2, Math.Ceiling(1.0001) = 2 Math.Ceiling(-1.5) = -1, Math.Ceiling(-1.99999) = -1 ... Is what you are looking for? PS: If it is, I'll start hiding my savings under my bed :laugh:

            D 1 Reply Last reply
            0
            • E elizas

              We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value. For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of Math.Round() method. Math.Round can also take a parameter known as MidPointRounding which helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero. For example, when someone do the rounding of 8.5 . MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9). http://www.mindfiresolutions.com/MidPointRounding-for-MathRound-method-in-Net-Application-293.php[^]

              Cheers, Eliza

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #6

              Not a horror by any means. (unless you have roundophobia, I guess)

              Agh! Reality! My Archnemesis![^]
              | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.

              1 Reply Last reply
              0
              • F fabrice leal

                Use the Math.Ceiling(1.5) = 2, Math.Ceiling(1.0001) = 2 Math.Ceiling(-1.5) = -1, Math.Ceiling(-1.99999) = -1 ... Is what you are looking for? PS: If it is, I'll start hiding my savings under my bed :laugh:

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                Well I was thinking more in terms of being able to say something like MidPointRounding.Up, or something like that. I'm basically lazy and prefer to rely on other people to do the grunt work and write the boring little stuff. It comes into things like currency trading for example. If the bank is calculating the rate at which they want to sell you something (dollars, for example) and they need to round it to a certain number of decimals, they will round it in such a way that is favourable to them. It may be a small thing on each trade, but it adds up. When they are doing things like calculating interest payments, they use "traditional" rounding.

                1 Reply Last reply
                0
                • D David Skelly

                  Not really relevant to this forum, but it has caught my attention. Are these really the only two options for rounding in .NET? What if I want (for example) to always round up (i.e. 1.5 becomes 2, but -1.5 becomes -1). That sounds a bit odd at first, but in some banking applications I have worked on that was how it worked because you rounded in the bank's favour. (Banks never miss a trick when it comes to money.)

                  J Offline
                  J Offline
                  Jeremy Tierman
                  wrote on last edited by
                  #8

                  Please tell which banks these were for so I can bank elsewhere! :cool:

                  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