Round a number to the next HIGHEST 10

16,349

Solution 1

if(rangeMax % 10 !=0)
   rangeMax = (rangeMax - rangeMax % 10) + 10;

You could also use Math.Round() with MidpointRounding.AwayFromZero using a decimal number (otherwise integer division will truncate fractions):

decimal number = 55M;
decimal nextHighest = Math.Round(number/ 10, MidpointRounding.AwayFromZero) * 10;

Solution 2

If you want to go up to the next 10, you can use Math.Ceiling as follows:

rangeMax = (int)(Math.Ceiling((decimal)rangeMax / 10) * 10);

If you need to generalize to go to the next n (for example 5 here) you can do:

int n = 5;    
rangeMax = (int)(Math.Ceiling((decimal)rangeMax / n) * n);

Solution 3

Something which might help is to divide the number by 10. This should round it to the nearest integer. Then multiply it by 10 again to get the number rounded to the nearest 10

Solution 4

I use THIS:

public static double RoundTo10(double number)
        {
            if (number > 0)
            {
                return Math.Ceiling(number / 10) * 10;
            }
            else
            {
                return Math.Floor(number / 10) * 10;
            }
        }
Share:
16,349
Archaon6044
Author by

Archaon6044

Updated on June 04, 2022

Comments

  • Archaon6044
    Archaon6044 almost 2 years

    I have a need to create a graph, where the scale of the Y-axis changes depending on the data input into the system. Conceivably this scale could be anywhere from 0-10, 0-100, or even have bottom limit of thousands and an upper limit of millions.

    To properly determinethe scale of this axis, I need to work out the ratio of Points to Pixels (based on graph height/range). Now a graphs' axis never start at the lowest value and go to the highest, usual practice is to go to the next nearest 2, 5 or 10 (above for upper limit, and below for lower) depending on the range of values.

    So what I'd like to know is how to take the max value from the data set, and round it up to the nearest 10. for clarification, the input values will always be integers.

    what i have now is this

            if ((rangeMax < 10) && (rangeMax > 5))
                rangeMax = 10;
            else if (rangeMax < 5)
                rangeMax = 5;
    

    Which is only useful for values less than 10, and doesn't allow the flexibility required to be truly dynamic. Ultimately this graph will be auto-generated during a page load event, with no user input.

    I've read around a bit, and people talk about things like the modulus operator (%), but I can't find any reliable information about it's use, and talk of Math.Ceiling and Math.Round, but these go to the next nearest whole number, which isn't quite there, and don't seem to help much at all when dealing with integers anyway. Any suggestions, pointers or help greatly appreciated.

    i did find a similar question asked here How can i get the next highest multiple of 5 or 10 but i don't know java, so i can't understand any of what was said.

    Cheers

  • harold
    harold over 12 years
    The ceil is doing nothing this way - it's a nice truncating integer division.
  • Jon Egerton
    Jon Egerton over 12 years
    @harold: That's true! - and the truncation should be sufficient.
  • harold
    harold over 12 years
    Yes well that rounds down.. (or rather, towards zero)
  • Archaon6044
    Archaon6044 over 12 years
    that would probably be more useful at the bottom end of the scale, becuase that will need to round down, away from the rangeMin value. that's pretty much how i had it planned anyway. cheers though
  • Archaon6044
    Archaon6044 over 12 years
    cheers, the MidpointRounding method seems to work, at least it does with my small, tamer set of test data. but i'm not quite clear on what your first method does, or how it works, with my test data it always steps straight over it
  • Archaon6044
    Archaon6044 over 12 years
    never mind, i've gone over it a few times with various sets of test data, i think i see how it works. cheersfor the answer, we have a winner
  • Jon Egerton
    Jon Egerton over 12 years
    @Archaon6044: The updated code will round up as required. To round down you can change the code from Math.Ceiling to Math.Floor
  • wruckie
    wruckie almost 10 years
    your code rounds down to nearest 10, not to next HIGHEST 10
  • Georg Patscheider
    Georg Patscheider over 4 years
    Warning: This will only work if you want to round to multiples of 10. Consider the value 2671.875 and stepsize 50. I would expect the rounded value 2700. 2671.875/50 = 53.4375, which rounds to 53. But 53x50 = 2650.