22

Is there any way to round up decimal value to its nearest 0.05 value in .Net?

Ex:

7.125 -> 7.15

6.66 -> 6.7

If its now available can anyone provide me the algo?

Bryan Menard
  • 13,234
  • 4
  • 31
  • 47
Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162

5 Answers5

32

How about:

Math.Ceiling(myValue * 20) / 20
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
  • predator4: That seems to be what the OP wants, with the `6.66 -> 6.7` example. – caf Sep 19 '09 at 13:36
  • Sorry. Fair enough, I've missunderstood the round up thing. To reduce confusion I've deleted my comment. – user37325 Sep 19 '09 at 14:08
  • 1
    It should be noted that while this is a very good solution for the problem of rounding to arbitrary fractions, for decimal place rounding Math.Round should be used (just in case anyone comes looking at this question for a solution to more standard rounding). – ICR Sep 19 '09 at 14:32
  • 8
    Just as a warning, this can go wrong if the input is within a factor of 20 of the max value of a decimal (so, greater than about 4*10^27 for the decimal type). You can get around this by subtracting Math.floor, rounding the non-integer part, and then adding it back to the floor value. But since the original questioner's use is for a tax calculation, I doubt it matters unless it has to work in Zimbabwe... – Steve Jessop Sep 19 '09 at 14:33
  • @Steve, lol! and @predator4/user37325, your comment deletion only increased my confusion – Patrick McDonald Apr 29 '11 at 17:08
  • There is a flaw in this code that will cause rounding issues, trying the value 1.41 incorrectly yields 1.5 – Woot4Moo Jun 21 '13 at 21:35
  • 9
    A more straightforward way (and easier to change the round-to step) is to Math.Ceiling(myValue / 0.05) * 0.05 – Andriy Volkov Aug 15 '13 at 12:47
12

Use this:

Math.Round(mydecimal / 0.05m, 0) * 0.05m;

The same logic can be used in T-SQL:

ROUND(@mydecimal / 0.05, 0) * 0.05

I prefer this approach to the selected answer simply because you can directly see the precision used.

Community
  • 1
  • 1
Marc
  • 9,012
  • 13
  • 57
  • 72
7

Something like this should work for any step, not just 0.05:

private decimal RoundUp (decimal value, decimal step)
{
    var multiplicand = Math.Ceiling (value / step);
    return step * multiplicand;
}
Andriy Volkov
  • 18,653
  • 9
  • 68
  • 83
2

Math..::.Round Method (Decimal, Int32, MidpointRounding)

Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.

   Math.Round(1.489,2,MidpointRounding.AwayFromZero)
Fredou
  • 19,848
  • 10
  • 58
  • 113
0

I could not get proper rounding in most of the formulas

This one "rounds" to nearest

float roundFloat(float value, float toNearest) {
            float divVal = (1 / (toNearest == 0 ? 1 : toNearest));
            return ((float)(Math.Round(value * divVal)) / divVal);
        }

Result:

roundFloat(2, 0.125F); -> 2
roundFloat(2.11, 0.125F); -> 2.125
roundFloat(2.22, 0.125F); -> 2.25
roundFloat(2.33, 0.125F); -> 2.375
roundFloat(2.44, 0.125F); -> 2.5
roundFloat(2.549999, 0.125F); -> 2.5
roundFloat(2.659999, 0.125F); -> 2.625
roundFloat(2.769999, 0.125F); -> 2.75
roundFloat(2.879999, 0.125F); -> 2.875
roundFloat(2.989999, 0.125F); -> 3

Example 0.125 nearest rounding

 2.000 
 2.125 
 2.250 
 2.375 
 2.500 
 2.625 
 2.750 
 2.875 
 3.000 
Tejasvi Hegde
  • 2,694
  • 28
  • 20