cs0815
cs0815

Reputation: 17388

double to certain string format

I have this simplified method:

private string GetStringValue(object Value)
{
    return ((double)Value).ToString();
}

which spews out:

1.8E-09

I intend to get this format though:

1.8e-009

Is this easily achievable?

Upvotes: 0

Views: 105

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1499770

Looking at the documentation for custom numeric format strings, I think you want:

// Separate variable just for clarity
double number = (double) Value;
return number.ToString("0.###e+000");

(Use 0.###E-000 if you only want the symbol for negative exponents.)

Upvotes: 6

Jason Meckley
Jason Meckley

Reputation: 7591

return string.Format("{0:0.###E+000}", value);

Upvotes: 0

Eterm
Eterm

Reputation: 1808

You need to use String.Format and use the right format string.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

This should help with decimal format strings.

So

(double)Value.ToString("E")

would do it for en-US.

Upvotes: 1

Related Questions