-5

Need to add 5 digit 0 before an integer name as recordnumber in an integer. and it is using in a loop which is adding record number in the word document as well. PLease provide any solution. i have tried following things but not working according to:

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();





     }
   } 
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • `recordnumber.tostring("00000");` doesn't do anything by itself, you should do it like `stringArray1[i] = recordnumber.ToString("000000") + "DA01" + customerId + "...` – Ulugbek Umirov Jun 09 '14 at 12:17

2 Answers2

6

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

or

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

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
0

Just use the following code

var formatted = recordnumber.ToString("d5")
Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75