【问题标题】:Format Double to fixed Width C#将 Double 格式化为固定宽度 C#
【发布时间】:2015-08-24 14:57:51
【问题描述】:

我的要求如下:

我有一个双号。它可以是积极的,也可以是消极的。我想用 String.Format() 来格式化。

输出数(正(无符号)/负(包括符号)/零)应为固定宽度:11 所有数字都应该有 2 位小数。

例子:

1.2     00000001.20
.2      00000000.20
-.2     -0000000.20
12      00000012.00
0       00000000.00
-0      00000000.00
12.555  00000012.55

基本上,我想要的是固定宽度(11 位)。 2 小数点。没有积极的迹象。负号包含在固定宽度中。

我尝试过的:

{0:00000000.00;0:0000000.00;0:00000000.00}

请告诉我这样做的正确方法是什么。

【问题讨论】:

  • 应该只是{0:00000000.00;-0000000.00}
  • 谢谢!!!我忘了我可以简单地使用 - 来格式化负片。但是如果我不格式化负号,它不应该按原样输出吗?我的意思是 0.0000000.00 应该给出宽度为 11 的负数,包括符号。
  • 不,“宽度”包括你的 - 符号,所以如果它是一个正数,它的 10 位数字 + 1 个小数,如果它的负数,它的 9 个数字、1 个小数和 1 个负号。在您的示例中,负号占据了一个数字位置,这意味着您需要不同的格式字符串。
  • @Madeyedexter 不,如果您指定“负”格式,则必须在输出中包含负号。这使您可以通过仅以输出格式反转符号来做“聪明”的思考:{-0.0000,0.0000}
  • @DStanley 所以如果我不把 - 放在否定部分的格式中, - 将被剥离并用零替换?

标签: c# formatting format


【解决方案1】:

您可以使用部分修饰符来提供所需的输出:

string format = "{0:00000000.00;-0000000.00}";

Console.WriteLine(string.Format(format, 1.2));
Console.WriteLine(string.Format(format, .2));
Console.WriteLine(string.Format(format, -.2));
Console.WriteLine(string.Format(format, 12.0));
Console.WriteLine(string.Format(format, -0.0));
Console.WriteLine(string.Format(format, 12.555));

打印出来:

00000001.20 00000000.20 -0000000.20 00000012.00 00000000.00 00000012.56

基本上是{position:postive format;negative format;zero format}

注意:位置仅用于格式化字符串,您也可以将其与double.ToString(string)一起使用,而无需“位置”元素; 如果未提供零格式,则使用正格式部分的格式来格式化零。

有关; 部分格式的更多详细信息,请参阅https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx

【讨论】:

  • 我知道部分格式。我已经在我的问题中提到了它。我的主要问题是将数字格式化为固定宽度。感谢罗恩的回答!
  • 我不仅对您而且对以后遇到此问题的任何人都尽量做到彻底,因此解释它不一定针对您,只是作为对其他人的一般“操作方法”。很高兴它对你有用。
【解决方案2】:
string.Format("{0:00000000.00}", 1.2) 

适合我

【讨论】:

  • 用负数试试,你会得到 11 位数字 + - 号,他需要 10 位数字和 - 号。
  • 您可以使用 if (value
【解决方案3】:

对于负数,您需要第二个格式部分。如果您需要重用它,将其声明为 Func 或扩展方法可能会有所帮助。像这样:

using System;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<double, string> toFixedWidth = (d) => { 
                return string.Format("{0:00000000.00;-0000000.00}", d); 
            };

            foreach (var d in new double[] { 1.2, 0.2, -0.2, 12, 0, -0, 12.555 })
            {
                Console.WriteLine(toFixedWidth(d));
            }
        }
    }
}

00000001,20
00000000,20
-0000000,20
00000012,00
00000000,00
00000000,00
00000012,56
Press any key to continue . . .

【讨论】:

  • 我想说这完全取决于预期的用途。
【解决方案4】:
double d = -12.365;
string s = d.ToString("0:00000000.00");   //-0:00000012.37
string sOut = d.ToString("00000000.00");  //-00000012.37

【讨论】:

  • 我认为你在小数点左/右的位数上相差甚远。
【解决方案5】:

这是一个通用函数。它的工作原理与其他答案相同,但它根据length(总长度)和decimals 参数动态构建格式化字符串。

public static string FormatDoubleAsFixedLengthString(double value, int length, int decimals)
{
    int wholeDigits = length - decimals;

    // Provide a spot for the negative sign if present.
    if (value < 0)
    {
        wholeDigits--;
    }

    string wholeDigitsFormat = new String('0', wholeDigits);
    string decimalDigitsFormat = new String('0', decimals);
    string format = $"{{0:{wholeDigitsFormat}.{decimalDigitsFormat}}}";
    return string.Format(format, value);
}

虽然我们在这里,但这里的函数是相同的,但对于 整数

public static string FormatIntAsFixedLengthString(int value, int length)
{
    // Provide a spot for the negative sign if present.
    if (value < 0)
    {
        length--;
    }
    return string.Format($"{{0:D{length}}}", value);
}

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 2012-01-15
    • 2012-01-06
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多