Q:

Double.ToString() with limited number of characters

Say I have some doubles:
12
10145
-0.00000000001455744121548787
100122455471000245487.05

I want to be able to converts them to strings with at most 9 characters. The output would be something like:
12
10145
-1.45e-11
1.0012e21

Is there an easy way to do this?
For the two first numbers, I do not want the output to be:
    1.2e1
    1.0145e4

A:

Looks like you want to use String.Format for your formatting.
You would first need to check if you need to use scientific notation (i.e. if number is greater than 10^7, between -10^-7 and 10^-7), but the way you print out a number to be in scientific notation with at most 6 digits after the decimal is:
String.Format("0.0######e+###", myDouble);

