Double to string conversion without scientific notation

前端 未结 17 1320
無奈伤痛
無奈伤痛 2020-11-22 15:33

How to convert a double into a floating-point string representation without scientific notation in the .NET Framework?

\"Small\" samples (effective numbers may be of

相关标签:
17条回答
  • 2020-11-22 15:47

    I could be wrong, but isn't it like this?

    data.ToString("n");
    

    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

    0 讨论(0)
  • 2020-11-22 15:49
    string strdScaleFactor = dScaleFactor.ToString(); // where dScaleFactor = 3.531467E-05
    
    decimal decimalScaleFactor = Decimal.Parse(strdScaleFactor, System.Globalization.NumberStyles.Float);
    
    0 讨论(0)
  • 2020-11-22 15:56

    In the old days when we had to write our own formatters, we'd isolate the mantissa and exponent and format them separately.

    In this article by Jon Skeet (https://csharpindepth.com/articles/FloatingPoint) he provides a link to his DoubleConverter.cs routine that should do exactly what you want. Skeet also refers to this at extracting mantissa and exponent from double in c#.

    0 讨论(0)
  • 2020-11-22 15:57

    The obligatory Logarithm-based solution. Note that this solution, because it involves doing math, may reduce the accuracy of your number a little bit. Not heavily tested.

    private static string DoubleToLongString(double x)
    {
        int shift = (int)Math.Log10(x);
        if (Math.Abs(shift) <= 2)
        {
            return x.ToString();
        }
    
        if (shift < 0)
        {
            double y = x * Math.Pow(10, -shift);
            return "0.".PadRight(-shift + 2, '0') + y.ToString().Substring(2);
        }
        else
        {
            double y = x * Math.Pow(10, 2 - shift);
            return y + "".PadRight(shift - 2, '0');
        }
    }
    

    Edit: If the decimal point crosses non-zero part of the number, this algorithm will fail miserably. I tried for simple and went too far.

    0 讨论(0)
  • 2020-11-22 15:58

    try this one:

    public static string DoubleToFullString(double value, 
                                            NumberFormatInfo formatInfo)
    {
        string[] valueExpSplit;
        string result, decimalSeparator;
        int indexOfDecimalSeparator, exp;
    
        valueExpSplit = value.ToString("r", formatInfo)
                             .ToUpper()
                             .Split(new char[] { 'E' });
    
        if (valueExpSplit.Length > 1)
        {
            result = valueExpSplit[0];
            exp = int.Parse(valueExpSplit[1]);
            decimalSeparator = formatInfo.NumberDecimalSeparator;
    
            if ((indexOfDecimalSeparator 
                 = valueExpSplit[0].IndexOf(decimalSeparator)) > -1)
            {
                exp -= (result.Length - indexOfDecimalSeparator - 1);
                result = result.Replace(decimalSeparator, "");
            }
    
            if (exp >= 0) result += new string('0', Math.Abs(exp));
            else
            {
                exp = Math.Abs(exp);
                if (exp >= result.Length)
                {
                    result = "0." + new string('0', exp - result.Length) 
                                 + result;
                }
                else
                {
                    result = result.Insert(result.Length - exp, decimalSeparator);
                }
            }
        }
        else result = valueExpSplit[0];
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 15:59

    My solution was using the custom formats. try this:

    double d;
    d = 1234.12341234;
    d.ToString("#########0.#########");
    
    0 讨论(0)
提交回复
热议问题