How to format floating point value with fix number of digits?

扶醉桌前 提交于 2020-01-30 13:02:47

问题


Is it possible in C# to format a double value with double.ToString in a way that I have always a fixed number of digits, no matter on which side of the decimal point?

Say I wish 6 digits, I want to have these results:

  • 0.00123456789 gives "0.00123"
  • 1.23456789 gives "1.23457"
  • 123.456789 gives "123.457"
  • 0.0000000123456789 gives "0.00000"
  • 12345678.9 gives "12345679" (on overflow I want to see all digits left of decimalpoint)
  • 4.2 gives "4.20000"

I'm experimenting with double.ToString, but cannot find any suitable format string.

Already tried "G6" (gives sometimes exponential format), "F6" (comes close, but 0.123456789 gives "0.123457" which are 7 digits).


回答1:


I think some of your examples are wrong. But I still think that I understand what you want to achieve.

I made an extension method.

public static class StringExtensionMethods
{
    public static string ToString(this double d, int numberOfDigits)
    {
        var result = "";

        // Split the number.
        // Delimiter can vary depending on locale, should consider this and not use "."
        string[] split = d.ToString().Split(new string[] { "." }, StringSplitOptions.None);
        if(split[0].Count() >= numberOfDigits)
        {
            result = split[0].Substring(0, numberOfDigits);
        }
        else
        {
            result = split[0];
            result += ".";
            result += split[1];

            // Add padding.
            while(result.Count() < numberOfDigits +1)
                result += "0";

            result = result.Substring(0, numberOfDigits + 1);
        }

        return result;
    }
}

I ran it with your examples:

        double d0 = 0.00123456789;
        double d1 = 1.23456789;
        double d2 = 123.456789;
        double d3 = 0.0000000123456789;
        double d4 = 12345678.9;
        double d5 = 4.2;

        Console.WriteLine(d0.ToString(6));
        Console.WriteLine(d1.ToString(6));
        Console.WriteLine(d2.ToString(6));
        Console.WriteLine(d3.ToString(6));
        Console.WriteLine(d4.ToString(6));
        Console.WriteLine(d5.ToString(6));

This is the output:

0.00123 1.23456 123.456 1.23456 123456 4.20000

I don't think this is the best way to solve it, but I like extension methods.

DoubleConverter class: http://1drv.ms/1yEbvL4




回答2:


If your goal is to avoid "jumping" of the decimal point:

  1. Use g formating, this does the most sensible thing to do
  2. See where the decimal point is in your resulting string
  3. pad with spaces at the beginning to align the column at the decimal point



回答3:


As I understand, there is no predefined format that does what I need. So for everyone who is interested, here is the function I ended up with:

public string FormatValue(double d, int noOfDigits)
    {
        double abs = Math.Abs(d);
        int left = abs < 1 ? 1 : (int)(Math.Log10(abs) + 1);

        int usedDigits = 0;

        StringBuilder sb = new StringBuilder();
        for(; usedDigits < left; usedDigits++)
        {
            sb.Append("0");
        }
        if(usedDigits < noOfDigits)
        {
            sb.Append(".");
            for(; usedDigits < noOfDigits; usedDigits++)
            {
                sb.Append("0");
            }
        }
        return d.ToString(sb.ToString());
    }


来源:https://stackoverflow.com/questions/28217098/how-to-format-floating-point-value-with-fix-number-of-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!