Prev: Design question
Next: Unmanaged code interop!
From: Peter Duniho on 30 Jan 2010 12:59 Arne Vajhøj wrote: > The question was: > > #How do I round up a number to the nearest 0.05? True. Note the word "up" in the above sentence. > 16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded > to nearest 0.05 is 6.65. True. But, 6.66 rounded *UP* to the nearest 0.05 is 6.70. > At least according to normal understanding of rounding > to nearest 0.05. > > I very much hope that my code do return those results. [...] Your code works fine for the problem you were trying to solve. But I believe you have misunderstood what the OP is asking for. Pete
From: Arne Vajhøj on 30 Jan 2010 14:26
On 30-01-2010 12:59, Peter Duniho wrote: > Arne Vajhøj wrote: >> The question was: >> >> #How do I round up a number to the nearest 0.05? > > True. Note the word "up" in the above sentence. > >> 16.489 rounded to nearest 0.05 is 16.50 and 6.66 rounded >> to nearest 0.05 is 6.65. > > True. But, 6.66 rounded *UP* to the nearest 0.05 is 6.70. > >> At least according to normal understanding of rounding >> to nearest 0.05. >> >> I very much hope that my code do return those results. [...] > > Your code works fine for the problem you were trying to solve. But I > believe you have misunderstood what the OP is asking for. Possible. Normally one rounds either up or down or to nearest. I let nearest take precedence over up in my interpretation of the question. If it is up then: return ((decimal)((int)(x * n + 0.50m))) / n; should be replaced by: return Math.Ceiling(x * n) / n; Arne |