Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a number to the next HIGHEST 10

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

like image 246
Archaon6044 Avatar asked Dec 30 '11 13:12

Archaon6044


People also ask

How do you round a number to the nearest 10?

Rule I. While rounding off to the nearest ten if the digit in the units place is between 0 – 4 i.e., < 5 then the unit digit is replaced by 0. Rule II. If the digit in the units place is 5 to 9 i.e., > or 5 but < 10, then the units place is replaced by '0' and the tens place increased by 1.

Is 5 rounded up or down?

“If the digit is less than 5, round the previous digit down; if it's 5 or greater, round the previous digit up.”


1 Answers

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;
like image 164
BrokenGlass Avatar answered Sep 23 '22 17:09

BrokenGlass