Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将双精度格式设置为固定宽度C#_C#_Formatting_Format - Fatal编程技术网

将双精度格式设置为固定宽度C#

将双精度格式设置为固定宽度C#,c#,formatting,format,C#,Formatting,Format,我的要求如下: 我有一个双倍号码。它可以是积极的,也可以是消极的。我想使用String.Format()对其进行格式化 输出数字(正(无符号)/负(含符号)/零)应为固定宽度:11 所有数字都应有两位小数 示例: 1.2 00000001.20 .2 00000000.20 -.2 -0000000.20 12 00000012.00 0 00000000.00 -0 00000000.00 12.555 00000012.55 基本

我的要求如下:

我有一个双倍号码。它可以是积极的,也可以是消极的。我想使用String.Format()对其进行格式化

输出数字(正(无符号)/负(含符号)/零)应为固定宽度:11 所有数字都应有两位小数

示例:

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}
请告诉我做这件事的正确方法是什么

string.Format("{0:00000000.00}", 1.2) 

对我来说效果很好

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

double d = -12.365;
string s = d.ToString("0:00000000.00");   //-0:00000012.37
string sOut = d.ToString("00000000.00");  //-00000012.37
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 1.20 0.20 -0000000.20 12 0 12.56 基本上是
{位置:正格式;负格式;零格式}

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


有关
的更多详细信息,请参阅部分格式。

负数需要第二个格式部分。如果需要重用它,将其声明为
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));
            }
        }
    }
}

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

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);
}
公共静态字符串格式DoubleAsFixedLengthString(双值、整数长度、整数小数)
{
int wholeDigits=长度-小数;
//如果出现负符号,则为其提供一个位置。
如果(值<0)
{
全数字--;
}
字符串wholeDigitsFormat=新字符串('0',wholeDigits);
字符串小数位数格式=新字符串('0',小数);
字符串格式=$“{{0:{wholeDigitsFormat}.{decimalDigitsFormat}}”;
返回string.Format(格式、值);
}
当我们在这里的时候,这里有相同的函数,但对于整数:

公共静态字符串格式IntasFixedLengthString(int值,int长度)
{
//如果出现负符号,则为其提供一个位置。
如果(值<0)
{
长度--;
}
返回string.Format($“{0:D{length}}”,值);
}

应该是
{0:00000000.00;-0000000.00}
谢谢!!!我忘了我可以简单地用a-来格式化负片。但是如果我不格式化负号,它不应该按原样输出吗?我的意思是0.0000000.00应该给出宽度11的负数,包括符号。不,“宽度”包括你的-符号,所以如果它是正数,它的10位数+1个小数点,如果它是负数,它的9位数,1个小数点,和1个负号。在您的示例中,负号占据一个数字位置,这意味着您需要不同的格式字符串。@Madeyedexter否,如果您指定“负号”格式,则必须在输出中包含负号。这让你可以通过在输出格式中反转符号来进行“聪明”的思考:
{-0.0000,0.0000}
@DStanley,所以如果我不在负片格式部分中加上-,那么-,将被剥离并替换为零?用负数试试,你会得到11位+符号,他需要10位数字和-符号。您可以使用if(值<0)。。其他的这不是一个好方法,但很管用。我想你的小数点后左右的位数相差太远了。我知道该部分的格式。我在问题中已经提到了这一点。我的主要问题是将数字格式化为固定宽度。谢谢你的回答,罗恩!我试着不仅对你,而且对以后遇到这个问题的任何人都要彻底,所以解释这个问题不一定是针对你的,只是作为对其他人的一般“指导”。很高兴它对你有用。我认为这取决于它的预期用途。
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);
}