QuickNote: Using Logs to get order of a number

Just had to scale a graph axis the other day depending on how large a specific value was – It needed to be scaled to be rounded up according to the order. It took me a while to remember about using logs – so just posting this code as a quick reminder to myself.

private int GraphAxisMax(int heightAxisMax)
    double round = Math.Log10(heightAxisMax);
    round = Math.Floor(round);
    round = Math.Pow(10, round);
    int roundCoeff = Convert.ToInt32(round);
    heightAxisMax = (Convert.ToInt32(Math.Ceiling((decimal)(heightAxisMax * 1.1 / roundCoeff))) * roundCoeff);
    return heightAxisMax;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>