MidPointRounding for Math.Round() method in .Net Application
-
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
-
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
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.
-
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
Or make it a blog entry.
-
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
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.)
-
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.)
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:
-
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
-
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:
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.
-
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.)
Please tell which banks these were for so I can bank elsewhere! :cool: