Q:

Convert number to scientific notation and obtain exponent

How can I convert a number to scientific notation annd obtain the exponent? For example if I have 23582 and want to convert it to 2.3582 x 10^4 and then obtain the '4' which is the order of the number? (I'm using C#)

A:

Note that the result can be negative in case the number is less that 1 so we use Math.Floor to handle that:
int exponent = num == 0 ? 0 : (int)Math.Floor((Math.Log10(Math.Abs(num))));

