简体   繁体   中英

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?

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");

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM