Is it possible to format the float in an unscientific format?

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

I am looking for some combination of the "F" and "R" qualifiers. I want F not to use scientific style, but I also want R to use as little space as possible to represent the number exactly.

So, given 0.000000001, the line should be 0.000000001. Not 1E-09, not 0.000000001000.

Is it possible to tell the system to "make it a fixed point, but use the minimum digits necessary to indicate the exact number"?

If not, what would be a good workaround? I thought: use precision of 20, and then just crack the end of 0, if any. in line. Anything better?

Edit:

Here is the version I'm using. I really hoped there would be a format specifier that I could use to do this instead.

var s = f.ToString("F20");
if (s.Contains("."))
{
    s = s.TrimEnd('0').TrimEnd('.');
}
+3
source share
2 answers

Only significant digits after the decimal point, up to 10 days, will be displayed below.

var format = "#0.##########";

string.Format(1.23, format);
// 1.23

string.Format(1.23456, format);
// 1.23456

string.Format(1.230045, format);
// 1.230045

string.Format(1.2345678912345, format);
// 1.2345678912

The key here is that the denominator #outputs only the number, if significant.

I hope that at least it will go the way to what you want. If not, you can always write custom IFormatProvider.

+1
source

, " " . 2. , 1/3 10 0.33333333.... 2, . "r", , .

"F20", , "r". Math.PI 3.14159265358979. 3.1415926535897931.

, "r".

static string FormatMinDigits(double d)
{
    String r = d.ToString("r", System.Globalization.CultureInfo.InvariantCulture);
    if (Double.IsInfinity(d) || Double.IsNaN(d))
        return r;
    String us = r.TrimStart('-');
    int epos = us.IndexOf('E');
    string mantissa;
    int exponent;
    if (epos == -1)
    {
        mantissa = us;
        exponent = 0;
    }
    else
    {
        mantissa = us.Substring(0, epos);
        exponent = Int32.Parse(us.Substring(epos + 1));
    }
    int dotPos = mantissa.IndexOf('.');
    if (dotPos == -1)
        dotPos = mantissa.Length;
    mantissa = mantissa.Replace(".", "");
    string s;
    if (exponent > 0)
    {
        if (exponent + dotPos - mantissa.Length > 0)
            mantissa += new String('0', exponent + dotPos - mantissa.Length);
        s = mantissa.Insert(exponent + dotPos, ".").TrimEnd('.');
    }
    else if (exponent < 0)
    {
        if (-(exponent + dotPos) > 0)
            mantissa = new String('0', -(exponent + dotPos)) + mantissa;
        s = mantissa.Insert(0, "0.");
    }
    else
        s = mantissa.Insert(dotPos, ".").TrimEnd('.');
    if (d < 0)
        s = '-' + s;
    if (double.Parse(s, System.Globalization.CultureInfo.InvariantCulture) != d) // Since format "r", it should roundtrip.
        throw new Exception(string.Format("Internal error in FormatMinDigits: {0:r}", r));
    return s;
}
+1

Source: https://habr.com/ru/post/1752928/


All Articles