8

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

Community
  • 1
  • 1
Archaon6044
  • 105
  • 1
  • 2
  • 7
  • Positive integers only or also negative? If also negative, should they round towards zero or to negative infinity? – harold Dec 30 '11 at 13:05
  • 2
    Well, if you're writing in C# then knowing Java is not really necessary. But to round up an INTEGER to the next multiple of 5 the way you seem to be describing it with your code snippet you'd add 4, divide by 5, then multiply by 5. – Hot Licks Dec 30 '11 at 13:08
  • @harold in the current scope, only positive, but if this works out then the concept will get used elsewhere in the system – Archaon6044 Dec 30 '11 at 13:29
  • possible duplicate of [Built in .Net algorithm to round value to the nearest 10 interval](http://stackoverflow.com/questions/274439/built-in-net-algorithm-to-round-value-to-the-nearest-10-interval) – Alex Angas Jan 20 '14 at 03:01

5 Answers5

24
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;
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 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 Dec 30 '11 at 13:35
  • 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 – Archaon6044 Dec 30 '11 at 13:56
  • 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. – Georg Patscheider Oct 25 '19 at 09:20
4

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);
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 1
    The ceil is doing nothing this way - it's a nice truncating integer division. – harold Dec 30 '11 at 13:12
  • @harold: That's true! - and the truncation should be sufficient. – Jon Egerton Dec 30 '11 at 13:13
  • 2
    Yes well that rounds down.. (or rather, towards zero) – harold Dec 30 '11 at 13:14
  • 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 Dec 30 '11 at 13:27
  • @Archaon6044: The updated code will round up as required. To round down you can change the code from `Math.Ceiling` to `Math.Floor` – Jon Egerton Dec 30 '11 at 14:07
2

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;
            }
        }
Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
2

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

Simon Verbeke
  • 2,905
  • 8
  • 36
  • 55
0

you can try this....

            decimal val = 95;
        //decimal val =Convert.ToDecimal( textBox1.Text);
        decimal tmp = 0;
        tmp = (val % 10);
        //MessageBox.Show(tmp.ToString()+ "Final val:"+(val-tmp).ToString());
        decimal finval = val - tmp;
Manoj Savalia
  • 1,402
  • 3
  • 13
  • 36