views:

35

answers:

0

I'm trying to convert a float to a string without getting scientific (1.13E-8) style formatting.

I'm looking for some combination of the "F" and "R" specifiers. I want the F so that it does not use the scientific style, but I also want the R so that it uses as little space as necessary to exactly represent the number.

So given 0.000000001, the string version should be 0.000000001. Not 1E-09, and not 0.000000001000.

Is it possible to tell the system "make it fixed point, but use the minimum digits necessary to exactly specify the number"?

If not, what would a good workaround be? I was thinking: use a precision of 20 and then just hack off trailing 0's if there's a '.' in the string. Anything better?

Edit:

Here's the version I have been using. I was really hoping there would be a format specifier I could use to make it do this instead.

var s = f.ToString("F20");
if (s.Contains("."))
{
    s = s.TrimEnd('0').TrimEnd('.');
}