algorithm ideas for number shorten with 1 Million = 1M, 1 Thousand = 1K  etc.

Posted on 2012-09-11
Last Modified: 2012-09-11
I have a set of numbers to be plot as histogram chart. The Y scale need a short legend for divisor such as M = million, K = thousand .

I need a logic so that the y scale show a meaningful numbers - and also all data will be divided by that divisor.

The set of number changed randomly for each new plot - so algorithm need to work when there are large and small numbers plot together. - no log scale is necessary.

Numbers will not goes below 0 but can be as high as billions.

How should i calculate for divisor, e.g. using average number or max/min etc.
Question by:JSW21
    LVL 12

    Accepted Solution

    Having a range of results vary between 0 and billions is unusual, but you will ordinarily find the scale of the greatest number, then treat all members of the result set the same.

    Assuming a maximum value of x you could do this:

    var x=1234567890;  //the number to be evaluated
    var e=0;                    //to accumulate the scale
    var i=x;                     //a working variable

    //Find the number's scale or order of decimal magnitude
    while (i > 1) {

    //Once you have determined the scale for the maximum value in the set, treat all members of the set the same way.

    if (e>=9) {
         }else if (e>=6) {
         }else if (e>=3) {
    LVL 43

    Expert Comment

    >>Numbers will not goes below 0 but can be as high as billions.

    If that really means that ALL plots will be from zero upwards then use the suggestion by Amick.

    However if you mean you could have a range of eg. 1,000,000 to 1,000,200 - all values within a range of 200 but with a start of 1 million then you will need to find the minimum and maximum values and use the difference to calculate the actual intervals that make sense.

    ps. re Amicks suggestion you could also convert to a string and use the length.  eg.  "123456789" is 9 chars long = billions

    Author Closing Comment

    They are stock volume so they can get varies.

    So basically fine the largest magnitude then convert all other numbers.

    But One thing - I then need to make sure - Y scale support decimal point - or - it will show 0 on the scale. thanks.

    PS. Thanks for other comments but points were given earlier.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now