David Talbot
David Talbot

Reputation: 151

Format double value in custom scientific notation

I am formatting my values as follows:

string text = value.ToString("0.000E0##");

This seems to work for some numbers, i.e.:

0.1          -> 1.000E-1
0.12         -> 1.200E-1
0.123        -> 1.230E-1
0.1234       -> 1.234E-1
1.2E-34      -> 1.200E-34
1.23E-45     -> 1.230E-45

...but fails with others, i.e.:

0.12345      -> 1.234E-15 //INCORRECT

Why is this the case?

Upvotes: 1

Views: 460

Answers (2)

Paolo Tedesco
Paolo Tedesco

Reputation: 57222

I cannot find in the custom numeric format string specifiers what the ## after the E0 in the format string should mean, what do you want exactly?

I think that you should only use 0.000E0, which works as expected.

Upvotes: 2

Sai Avinash
Sai Avinash

Reputation: 4753

You could specify a custom format like so.

var num = 3147.3;
num.ToString("\\0.#####E0"); // "0.31473E4"

Hope this helps..

Upvotes: 1

Related Questions