Convert double to scientific notation with specific number after decimal points

user2089789 picture user2089789 · Sep 15, 2014 · Viewed 10.9k times · Source

I want to convert double to scientific notation like this:

-0.00752382528 => -.752383E-1

can i do this with .ToString() or Regex?

Answer

D Stanley picture D Stanley · Sep 15, 2014

You can either use the standard format string for scientific notation:

(-0.00752382528).ToString("E5")   // returns "-7.52383E-003"

or if you don't want the leading zeros in the exponent, use a custom string:

(-0.00752382528).ToString("0.00000E0")   // returns "-7.52383E-3"