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;

You May Also Like

About the Author: rnowik

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.