我正在尝试在字符串的左侧填充零。我需要生成以下格式的基本数字。由于某种原因,它打印的数字是这样的10,20。。。180例如
00100020003000400050。。。0180
逻辑
var col = 0;
for (int i = 1; i <= 18; i++)
{
col = i*10;
col.ToString().PadLeft(4, '0');
}
Console.WriteLine(col);
在将变量打印到屏幕之前,您没有将更新后的格式/值设置为变量。
var col = 0;
for (int i = 1; i <= 18; i++)
{
col = i*10;
// need to assign the value to the col or you can just print it directly to screen in the loop like line below this one
string formattedNumber = col.ToString().PadLeft(4, '0');
// or just print the formatted string
Console.WriteLine(col.ToString().PadLeft(4, '0'));
// or
Console.WriteLine(formattedNumber);
}
Console.WriteLine(col);