Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

OK, I've got a number (say 0.5004) and I using custom formatting I get into a string such as "5E-01". Now, my question is this, not using string methods (i.e. finding the index of "E" and use substring() to pull out the "-01" portion), is their a mathematical way to retrieve the integral value of "-01" in the scientific notated number? I'd like a method that would return the following values:

value s.n. return

0.5004 "5E-01" -1

5.004 "5E+01" +1

50.04 "5E+02" +2

etc.

Using the Math functions in C# is there a way to do this? It's been many, many years since I've taken a math course or cracked a math book, so this might be an easy question.

If there's not, would someone help me come up with the right algorithm using string methods? Thanks.

value s.n. return

0.5004 "5E-01" -1

5.004 "5E+01" +1

50.04 "5E+02" +2

etc.

Using the Math functions in C# is there a way to do this? It's been many, many years since I've taken a math course or cracked a math book, so this might be an easy question.

If there's not, would someone help me come up with the right algorithm using string methods? Thanks.

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

yuppers.... you need to use logs.

.5 = 5 * 10^ -1

5 = 5 * 10^ 1

50 = 5 * 10^ 2

val = x * 10^ n

log(val) = log(x) + n*log(10) -> log(10) = 1

log(val) = log(x) + n

log(val) - log(x) = n

log(val/x) = n

So....

If your value is 5,983,899 you know your scientific notation is going to be 5.983 899 E ??

Instead of doing all the multiplication or division you can simply log(5983899/5.983899) = 6

i don't know if there's anything build in (see the System.Math class)

if you really want to have a decimal equivalent of 5E-1 you can do something like this:

string text = "5E-01";

string[] values = text.ToLower().Split('e');

double val = double.Parse(values[0]);

int exponent = int.Parse(values[1]);

double ret = val * Math.Pow(10, exponent);

public static double GetExponent(double d) {

double j;

if(d >= 1)

for(j = d; j > 10.0; j /= 10);

else

for(j = d; j < 1; j *= 10);

return Math.Log10(d/j);

}

Hope that helps,

ZRH

public static string GetScientificNotation(doub

double j;

if(d >= 1)

for(j = d; j > 10.0; j /= 10);

else

for(j = d; j < 1; j *= 10);

return j + "E" + Math.Log10(d/j);

}

ZRH

But if your asking to get a double from a string with a number in scientific notation in it, you can just use the Double.Parse(string) method.

It supports scientific notation.

But besure to catch that pesky FormatException....

ZRH

the Log function in VB 6 is the natural logarithm which is the logarithm to the base e. The constant e is approximately 2.718282.

in order to get Log Base 10, define your own function, in a Module, like this:

Public Function Log10(X) as Double

Log10 = Log(X) / Log(10#)

End Function

then if you take the Integer value of the Log10(x) you will the the number you need.

AW

.

from a math standpoint, it doesn't matter if you use natural log or base 10 log (or log base 3, 7 9 42...etc, etc). The only thing that is imprtant is that every element uses a log.

The main reason for using log base 10 is because logBase10(10) = 1

If you have an equation 370.45 = 42^n you could use a log base 42 and do a few conversions

log42(370.45) = n * log42(42) => n * 1

log42(370.45) = n

n = 1.585-ish

To keep things simple I'd just go with whatever your programming language uses as a default for its log function and just do the algebra. Converting to log base 42 makes the problem much simpler if you are doing things long hand.

As long as you've got a computer doing the work, then you don't need to make the math easy.

And using Log 10 makes the answer to beaudetious' original question almost trivial.

True, you can convert from Ln to Log 10 quite easily, to get the correct exponent for Scientific notation, but Log10 makes it much easier.

AW

However, depending upon your maintainance programmer's "ability", the string method would probably be easier to understand.

Hope that helps,

ZRH

I'll have to side with ZRH on this one...

While the string manipulation answer might be easier to understand right off, it will always be a bit slower.

However, if you are only doing this once or twice in a program, then the 20 ms longer you have to wait is really trivial. If on the other hand you have to do this 1,000, 10,000 or a million times (in a loop) then I would *strongly* encourage using the mathematical method put forth by ZRH.

20ms * 1 million itterations comes to nearly 6 hours longer to use the string function over the math version.

ps> the additional 20 ms for the string funciton is a ficticious number. It could be more or less than that number. I have not actually tested the duration.

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial