Soul Reaver
Soul Reaver

Reputation: 2092

Double to string conversion (engineering format)

I'm looking for a method to use very specific string format:

 -2.71405E-03  0.00000E+00 -2.71405E-03 -2.71405E-03  0.00000E+00 -2.71405E-03
 -2.71405E-03 -2.71405E-03 -2.71405E-03 -2.71405E-03 -2.71405E-03 -2.71405E-03
 -2.71405E-03 -2.71405E-03 -2.71405E-03 -5.42809E-03 -2.71405E-03 -2.71405E-03

It is format used in UFF58 file. This format is described with FORTRAN format string E13.5, which means 13 (unlike in languages like C/C++, it's also upper bound) characters, 5 decimal digits. I've this code:

double d = -2.71405E-03;
d.ToString( "E5" ).Dump();

In LINQPad this gives output: -2.71405E-003.

I can't find any property of NumberFormatInfo class, that can limit size (character count used) of exponent. Any idea how to solve it with the change of format string or NumberFormatInfo?

Upvotes: 2

Views: 1476

Answers (2)

Tom Chantler
Tom Chantler

Reputation: 14951

If you need it to have 5 digits after the decimal point regardless, you can do this:

double d = -2.71405E-03;
d.ToString("0.00000E+00");

Upvotes: 3

Steve Wellens
Steve Wellens

Reputation: 20638

Try this:

string MyString = d.ToString("0.#####E-00");

Upvotes: 1

Related Questions