Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.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# 格式字符串/数字“;nnnn";_C#_String_Numbers_Format - Fatal编程技术网

C# 格式字符串/数字“;nnnn";

C# 格式字符串/数字“;nnnn";,c#,string,numbers,format,C#,String,Numbers,Format,我必须写一个数字的级数,每个数有5位。我的代码是: int count = 1; string labelCount = ""; foreach (var directory in folderList) { if (count < 10) { labelCount = "0000" + count.ToString(); } else if (count < 100) { labelCount = "000"

我必须写一个数字的级数,每个数有5位。我的代码是:

int count = 1;
string labelCount = "";
foreach (var directory in folderList)
{
    if (count < 10)
    {
        labelCount = "0000" + count.ToString();
    }
    else if (count < 100)
    {
        labelCount = "000" + count.ToString();
    }
    else if (count < 1000)
    {
        labelCount = "00" + count.ToString();
    }
    else if (count < 10000)
    {
        labelCount = "0" + count.ToString();
    }

    count++;
}
int count=1;
字符串labelCount=“”;
foreach(folderList中的var目录)
{
如果(计数<10)
{
labelCount=“0000”+count.ToString();
}
否则,如果(计数<100)
{
labelCount=“000”+count.ToString();
}
否则,如果(计数<1000)
{
labelCount=“00”+count.ToString();
}
否则,如果(计数<10000)
{
labelCount=“0”+count.ToString();
}
计数++;
}
但在我看来,它看起来并不那么好。是否有一种格式化数字的方法(将0xN添加到左侧)或这是唯一的方法?

请查看:

只需为
ToString
方法指定格式即可

var str = count.ToString("00000");

您可以通过执行以下操作来实现这一点:

string formatted = count.ToString();
for(int i = 0; i < count - 5; i++)
{
    formatted = "0" + formatted;
}
labelCount.Text = formatted;
string formatted=count.ToString();
对于(int i=0;i
编辑:Sry,我的错!应该是:

//..
for(int i = 0; i < 5 - count.ToString().Length; i++)
//..
/。。
for(int i=0;i<5-count.ToString().Length;i++)
//..

尝试下面的方法,它将帮助您

labelCount  = string.Format("{0:00000}", count);

有关所有格式,请参见此处:

像这样走怎么样

int count = 1;
string labelCount = "";

foreach (var directory in folderList)
{
   int i = 10000;
   while (count < i)
   {
       labelCount += 0;
       i /= 10;
   }

   labelCount += count.ToString();
   count++;
}
int count=1;
字符串labelCount=“”;
foreach(folderList中的var目录)
{
int i=10000;
而(计数
那么如果计数为10,则添加5个零?如果计数为100,则添加95个零?。。。但是要小心,
count
不是负数
000-42
真的很难看。在他的代码中,计数从
1开始,永远不会递减&你可以从上下文推断变量永远不会为负。好的一般性建议,我同意。是的,这是一般性建议
count.ToString(“00000”)
适用于所有整数,但如上所示的
PadLeft
仅适用于非负整数。
int count = 1;
string labelCount = "";

foreach (var directory in folderList)
{
   int i = 10000;
   while (count < i)
   {
       labelCount += 0;
       i /= 10;
   }

   labelCount += count.ToString();
   count++;
}