'How to convert from big number to short plus Exp?

Maybe I just didn't sleep enough today and can't think clearly enough today:

I have big numbers, and I have an array of Exp "big number names". I want to get the Exponent of the big number and then display the big number as a decimal value + big number label.

string[] exponent = 
    {
        "Mil",
        "Bil",
        "Tri",
        "Qua",
        "Qui",
        "Sex",
        "Sep",
        "Oct",
        "Non",
    };

double value = 1230000000;
if(value > 1000000)
{
  int pow = (int)(value / 1000000);
  res = value.ToString("#.##") + exponent[pow] ;
}

Expected output I want would be:

1.23Bil

but I'm clearly not converting value correctly.



Solution 1:[1]

Using the logarithm base 10 will get you the nearest power of 10 to the number, but you need to round to the nearest multiple of 3 of the power of ten (you really want the log base 1000).

Then you need to divide the value by that power of 1000 to get the matching mantissa:

string res;
if(value >= 1e6) {
  int pow = ((int)Math.Log10(value))/3;
  res = (value/Math.Pow(10, 3*pow)).Dump().ToString("#.##") + exponent[pow-2] ;
}
else
    res = value.ToString();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NetMage