First off, I note that you should always use decimal for this task; never use double . If you are using double stop what you are doing right now and fix your program so that you stop using a type designed for physics problems and start using a type designed for money problems to solve your money problem.
Up vote 1 down vote favorite share g+ share fb share tw.
I am looking for a way to round to the nearest dollar with the following stipulations: (If wholenumber.50 and above round up to the next whole number) (If wholenumber.49 and below round down to the current whole number) I have tried: Math. Round(wholenumber. Xx, MidpointRounding.
ToEven); This doesn't always round how I want for instance 1.5 = 2 and 2.5 = 2 as it rounds to nearest even number. I have also tried: Math. Round(wholenumber.
Xx, MidpointRounding. AwayFromZero); This always rounds up to the higher whole number. Is there any built in functionality for what I am trying to do or will I need to write my own custom method to check the number and do floor or ceil depending?
C# math rounding floor ceil link|improve this question edited 1 hour agoOlivier Jacot-Descombes11.1k2420 asked 1 hour agoBaxter31012 89% accept rate.
4 Using floating point arithmetics to store currency is never a good idea though.. – Jack 1 hour ago You can do Math. Round without a second parameter I think? That's the default rounding like you described.
– Mario De Schaepmeester 1 hour ago 2 MidpointRounding. AwayRoundZero is what you describe. It doesn't mean to round up all values, only midpoints (0.5).
– dtb 1 hour ago 1 @Jack: It is perfectly acceptable to use floating point arithmetic; you should avoid binary floating point arithmetic. The decimal type is a floating point type, it is just not a binary floating point type. As its name indicates, it is a decimal floating point type.
– Eric Lippert 1 hour ago You do not say what you want to happen for negative numbers. When you say "round up" on a negative number do you mean round up in magnitude -- away from zero -- or round up in value -- towards zero? – Eric Lippert 1 hour ago.
First off, I note that you should always use decimal for this task; never use double. If you are using double, stop what you are doing right now and fix your program so that you stop using a type designed for physics problems and start using a type designed for money problems to solve your money problem. Second, you are simply wrong when you say This always rounds up to the higher whole number.
It does not. It rounds to the nearest whole number, and if there is no nearest whole number because you are at a midpoint, then it chooses the whole number that is farther from zero. Try it, if you don't believe me: using System; class P { static void Main() { decimal buckFifty = 1.50m; decimal buckFortyNine = 1.49m; Console.
WriteLine(Math. Round(buckFortyNine, MidpointRounding. AwayFromZero)); Console.
WriteLine(Math. Round(buckFifty, MidpointRounding. AwayFromZero)); Console.
WriteLine(Math. Round(-buckFortyNine, MidpointRounding. AwayFromZero)); Console.
WriteLine(Math. Round(-buckFifty, MidpointRounding. AwayFromZero)); } } results are 1 2 -1 -2.
I see what you mean. I was thinking of it in the context of being at the midpoint. It always goes to the higher number when at the midpoint.
I am also using Decimal already. – Baxter 1 hour ago @Baxter: "It always goes to the higher number when at the midpoint. " - According to your question, that's exactly what you want... – BlueRaja - Danny Pflughoeft 1 hour ago Thanks, that set me straight.
– John H 1 hour ago Thanks that clears it up for me. – Baxter 1 hour ago 1 @Baxter: No. When at the midpoint it always goes to the number farther away from zero.
That's why the enum value is called MidpointRounding.AwayFromZero. If it always went to the higher number then it would be MidpointRounding.ToHigherNumber. -2 is further from zero than -1, but -1 is higher than -2.
– Eric Lippert 1 hour ago.
If you have only positive values, you can try (int)(decimal_number + 0.5m) or (long)(decimal_number + 0.5m).
Form Question: how I want for instance 1.5 = 2 and 2.5 = 2 as it rounds to nearest even number – L. B 1 hour ago 1 And what does this do for negative numbers? Is its behaviour the desired behaviour?
– Eric Lippert 1 hour ago You are right, I updated my answer. – Olivier Jacot-Descombes 1 hour ago.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.