Convert double to scientific notation with specific number after decimal points
Asked Answered
A

1

14

I want to convert double to scientific notation like this:

-0.00752382528 => -.752383E-1

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

Augustus answered 15/9, 2014 at 15:7 Comment(3)
-.752383E-1 is not proper scientific notation - even if it were, it does not match your original value.Barque
msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspxBrey
@DStanley, you'r right, but it's a conversion that is executed in an old software and i should rewrite it.Augustus
B
21

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"
Barque answered 15/9, 2014 at 15:11 Comment(2)
I know these string formatting but i have to convert as described in answer. i think i have to write a custom method to do that.Augustus
In your example using ToString("E5") which gives : -7,52383E-003 It will not be -.752383E-1 ? Seems like you are a factor of x10 off. -0.007.. vs -0.07 vs ToString("E") gives you the scientific notation and the number of decimals in "E5" is five decimals. No need for regex.Midland

© 2022 - 2024 — McMap. All rights reserved.