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

前端 未结 3 1995
借酒劲吻你
借酒劲吻你 2021-01-29 06:41

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?

Sa

相关标签:
3条回答
  • 2021-01-29 07:03

    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());
        }
    
    0 讨论(0)
  • 2021-01-29 07:10

    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
    0 讨论(0)
  • 2021-01-29 07:14

    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

    0 讨论(0)
提交回复
热议问题