39

I am trying to figure out how to round prices - both ways. For example:

Round down
43 becomes 40
143 becomes 140
1433 becomes 1430

Round up
43 becomes 50
143 becomes 150
1433 becomes 1440

I have the situation where I have a price range of say:

£143 - £193

of which I want to show as:

£140 - £200

as it looks a lot cleaner

Any ideas on how I can achieve this?

Richard Cook
  • 32,523
  • 5
  • 46
  • 71
dhardy
  • 993
  • 2
  • 11
  • 22

6 Answers6

85

I would just create a couple methods;

int RoundUp(int toRound)
{
     if (toRound % 10 == 0) return toRound;
     return (10 - toRound % 10) + toRound;
}

int RoundDown(int toRound)
{
    return toRound - toRound % 10;
}

Modulus gives us the remainder, in the case of rounding up 10 - r takes you to the nearest tenth, to round down you just subtract r. Pretty straight forward.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 12
    The `RoundUp` is broken. If you pass `20`, it gets rounded up to `30` which is most likely not what you want. You can either do a conditional test (`if ((toRound % 10) == 0) return toRound;`) or use an unconditional rounding, for example `return ((toRound + 9) / 10) * 10;` – DarkDust Dec 10 '13 at 09:00
  • 5
    Note that the logic above only works for positive numbers (as I just found out). – Adam Knights Dec 12 '13 at 14:01
  • 2
    Change to `Math.Abs(toRound) % 10` if you have negatives anywhere in price calc or otherwise. – Adam Knights Dec 12 '13 at 14:09
  • As stated in the first comment on your answer, `RoundUp(20)` returns `30`. – user247702 Jul 01 '15 at 15:49
16

You don't need to use modulus (%) or floating point...

This works:

public static int RoundUp(int value)
{
    return 10*((value + 9)/10);
}

public static int RoundDown(int value)
{
    return 10*(value/10);
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Might not give you what you want for very large numbers (e.g. Int32.MaxValue) – Joe Mar 01 '13 at 10:19
  • 1
    Yes, RoundUp will fail for numbers higher than (int32.MaxValue-10). Don't think that's an issue for prices in pounds though. And it's not possible to round those numbers up anyway, by any means (unless you return a long). – Matthew Watson Mar 01 '13 at 13:28
  • 1
    This worked well for me, and I like that it does not use modulus. Equivalent in Go: func RoundUp(v int) int { return 10 * ((v + 9) / 10) } func RoundDown(v int) int { return 10 * (v / 10) } – fiorix Dec 04 '15 at 19:17
  • 1
    This RoundDown implementation doesn't work for negative value. – Nic Foster Jul 13 '22 at 03:25
  • @NicFoster It's not required to work for negative numbers - it's for rounding prices. – Matthew Watson Jul 13 '22 at 07:33
13

This code rounds to the nearest multiple of 10:

int RoundNum(int num)
{
     int rem = num % 10;
     return rem >= 5 ? (num - rem + 10) : (num - rem);
}

Very simple usage :

Console.WriteLine(RoundNum(143)); // prints 140
Console.WriteLine(RoundNum(193)); // prints 190
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
5

A general method to round a number to a multiple of another number, rounding away from zero.

For integer

int RoundNum(int num, int step)
{
    if (num >= 0)
        return ((num + (step / 2)) / step) * step;
    else
        return ((num - (step / 2)) / step) * step;
}

For float

float RoundNum(float num, float step)
{
    if (num >= 0)
        return floor((num + step / 2) / step) * step;
    else
        return ceil((num - step / 2) / step) * step;
}

I know some parts might seem counter-intuitive or not very optimized. I tried casting (num + step / 2) to an int, but this gave wrong results for negative floats ((int) -12.0000 = -11 and such). Anyways these are a few cases I tested:

  • any number rounded to step 1 should be itself
  • -3 rounded to step 2 = -4
  • -2 rounded to step 2 = -2
  • 3 rounded to step 2 = 4
  • 2 rounded to step 2 = 2
  • -2.3 rounded to step 0.2 = -2.4
  • -2.4 rounded to step 0.2 = -2.4
  • 2.3 rounded to step 0.2 = 2.4
  • 2.4 rounded to step 0.2 = 2.4
Julian Declercq
  • 1,536
  • 3
  • 17
  • 32
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
4

Divide the number by 10.

number = number / 10;
Math.Ceiling(number);//round up
Math.Round(number);//round down

Then multiply by 10.

number = number * 10;
Julian Declercq
  • 1,536
  • 3
  • 17
  • 32
Sharun
  • 3,022
  • 6
  • 30
  • 59
1
public static int Round(int n)
        {
            // Smaller multiple 
            int a = (n / 10) * 10;

            // Larger multiple 
            int b = a + 10;

            // Return of closest of two 
            return (n - a > b - n) ? b : a;
        }
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Kurt Van den Branden Jan 16 '19 at 14:18
  • Doesn't work with negative numbers. – Nic Foster Jul 13 '22 at 03:37