如何在c#中给一个整数加5个零(控制台应用程序)

本文关键字:一个 整数 5个零 应用程序 控制台 | 更新日期: 2023-09-27 18:07:33

需要在整数名前加5个数字0作为整数中的recordnumber。它在一个循环中使用它也在word文档中添加记录号。请提供解决方案。我试过以下事情,但没有按照:

int recordnumber =1;
recordnumber.tostring("00000");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
    string[] stringArray1 = new string[2];
    string[] stringArray2 = new string[2];
    string[] stringArray3 = new string[2];
    //ArrayList al = new ArrayList();
    int customerId = 100000;
    int customeraccount = 100001;
    int accountnumber = 100000100;
    int recordnumber = 1; 

    for (int i = 0; i < 2; i++)
    {
        if (i == 0)
        {
            stringArray1[i] = "000001HE20140430CUSTOMER            01.00.00 'r'n" + recordnumber + "DA01" + customerId + "              00 8862019053100639390     00000-0085001-0                  BISP                     BISP                     BISP                     19830130BISP                                              BISP                                              00000000000                                                                                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    12345678910                                                                                                                                                                                                         12345678910 0120                  ";
        }
        else
        {
            if (i == 1)
            {
                stringArray1[i] = recordnumber + "DA01" + customerId + "              00 8862019053100639390     00000-0085001-0                  BISP                     BISP                     BISP                     19830130BISP                                              BISP                                              00000000000                                                                                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    12345678910                                                                                                                                                                                                         12345678910 0120                  'r'n002000FO20140430CUSTOMER";
            }
            else
            {
                stringArray1[i] = recordnumber + "DA01" + customerId + "              00 8862019053100639390     00000-0085001-0                  BISP                     BISP                     BISP                     19830130BISP                                              BISP                                              00000000000                                                                                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    BISP                                    12345678910                                                                                                                                                                                                         12345678910 0120                  ";
            }
        }
        if (i == 0)
        {
            stringArray2[i] = "000001HE20140430ACCOUNT             01.00.00'r'n" + recordnumber + "DA01120" + accountnumber + "001 20586BISP                                                                             88600";
        }
        else
        {
            if (i == 1)
            {
                stringArray2[i] = recordnumber + "DA01120" + accountnumber + "001 20586BISP                                                                             88600'r'n002000FO20140430ACCOUNT";
            }
            else
            {
                stringArray2[i] = recordnumber + "DA01120" + accountnumber + "001 20586BISP                                                                             88600";
            }
        }
        if (i == 0)
        {
            stringArray3[i] = "000001HE20140430ACCOUNT_REL         01.00.00'r'n" + recordnumber + "DA01" + accountnumber + "           120886083001001 20586";
        }
        else
        {
            if (i == 1)
            {
                stringArray3[i] = recordnumber + "DA01" + accountnumber + "           120886083002001 20586'r'n002000FO20140430ACCOUNT_REL";
            }
            else
            {
                stringArray3[i] = recordnumber + "DA01" + accountnumber + "           120886083002001 20586";
            }
        }

        //Convert.ToInt16(recordnumber);
        recordnumber++;
        accountnumber++;
        customerId++;
        customeraccount++;
    }


    StreamWriter file2 = new StreamWriter(@"d:'Tammeer_Imp0055.txt");

    file2.WriteLine(String.Join(Environment.NewLine, stringArray1));
    file2.WriteLine(String.Join(Environment.NewLine, stringArray2));
    file2.WriteLine(String.Join(Environment.NewLine, stringArray3));
    file2.Close();
    Console.ReadLine();


     }
   } 
}

如何在c#中给一个整数加5个零(控制台应用程序)

string newstring = yourstring.PadLeft(6, '0');

string formatted = yourstring.ToString("d6");

只需使用以下代码

var formatted = recordnumber.ToString("d5")