Prev: Design questionNext: Unmanaged code interop! From: Gregory A. Beamer on 29 Jan 2010 18:01 Here is an easy solution to wrap your head around: public static double RoundToPointOhFive(double input) { return Math.Round(input*20)/20; } This gives you the following table: 123 = 123 123.01 = 123 123.02 = 123 123.03 = 123.05 123.04 = 123.05 123.05 = 123.05 123.06 = 123.05 123.07 = 123.05 123.08 = 123.1 123.09 = 123.1 123.1 = 123.1 You can break it up even farther if you want to test it more. How did I get 20 here? 1. I need to get to the item I want to round on. In this case, multiply by 10 1230.6 2. I am rounding now on .5, so I have to make it 1. This is done by multiplying by 2. 2461.2 You then round 2461 And divide by 20 123.05 I assume this is what you mean when you say round to nearest .05? If so, you can determine if there is a better way to refactor this. ;-) -- Peace and Grace, Greg Twitter: @gbworld Blog: http://gregorybeamer.spaces.live.com ************************************************ | Think outside the box! | ************************************************ "Raj" wrote in message news:45B6044B-D832-4C87-8DC0-8A649FABBBCF(a)microsoft.com...> How do I round up a number to the nearest 0.05? From: Arne Vajhøj on 29 Jan 2010 22:31 On 29-01-2010 02:48, Raj wrote:> How do I round up a number to the nearest 0.05? There are multiple ways of doing that. One of them is: public static decimal FlexRound(decimal x, decimal unit) { int n = (int)(1 / unit); if(unit * n != 1.00m) { throw new ArgumentException("Invalid unit: " + unit); } return ((decimal)((int)(x * n + 0.50m))) / n; } Arne From: Raj on 30 Jan 2010 00:31 Try out the solution with these numbers: 16.489 should return 16.49 6.66 should return 6.70 etc., None of the solution does this, i am sorry! "Arne Vajhøj" wrote: > On 29-01-2010 02:48, Raj wrote: > > How do I round up a number to the nearest 0.05? > > There are multiple ways of doing that. > > One of them is: > > public static decimal FlexRound(decimal x, decimal unit) > { > int n = (int)(1 / unit); > if(unit * n != 1.00m) > { > throw new ArgumentException("Invalid unit: " + unit); > } > return ((decimal)((int)(x * n + 0.50m))) / n; > } > > Arne > > . > From: Peter Duniho on 30 Jan 2010 02:17 Raj wrote:> Try out the solution with these numbers: > 16.489 should return 16.49 Why not 16.50? How is 16.49 a multiple of 0.05 at all? > 6.66 should return 6.70 > etc., > > None of the solution does this, i am sorry! IMHO, your problem statement is poorly defined. But, assuming you mean that you always want numbers not a multiple of 0.05 to be rounded upwards to the next multiple of 0.05, what is wrong with the solution provided here: vanderghast wrote:> [...] To round-up-to the next 1/20 (unless it is already an exact > 1/20), rather use > > Math.Ceiling( 20*x ) / 20.0 Noting, of course, his caveat regarding the usefulness of such a rounding for values of type float or double. That's why Arne's solution uses the decimal type (though due to the ambiguous problem statement, it appears he solved a problem different from the one you actually are interested in). Pete From: Arne Vajhøj on 30 Jan 2010 10:08 On 30-01-2010 00:31, Raj wrote:> "Arne Vajhøj" wrote: >> On 29-01-2010 02:48, Raj wrote: >>> How do I round up a number to the nearest 0.05? >> >> There are multiple ways of doing that. >> >> One of them is: >> >> public static decimal FlexRound(decimal x, decimal unit) >> { >> int n = (int)(1 / unit); >> if(unit * n != 1.00m) >> { >> throw new ArgumentException("Invalid unit: " + unit); >> } >> return ((decimal)((int)(x * n + 0.50m))) / n; >> } > None of the solution does this, i am sorry! > Try out the solution with these numbers: > 16.489 should return 16.49 > 6.66 should return 6.70 > etc., The question was: #How do I round up a number to the nearest 0.05? 16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded to nearest 0.05 is 6.65. At least according to normal understanding of rounding to nearest 0.05. I very much hope that my code do return those results. If you have a different meaning of rounding to nearest 0.05, then please explain what it means. Arne First  |  Prev  |  Next  |  Last Pages: 1 2 3 Prev: Design questionNext: Unmanaged code interop!