Need help with Convert.ToInt32()...

WebAppDeveloper
WebAppDeveloper used Ask the Experts™
on
Hi,

I have the following code below that keeps knocking off the first 2 leading zeros (0). It looks like the Convert.ToInt32() is doing that. How can I retain the first 2 leading integer zeros (0) in my zipCode variable so that the result is 00210 instead of 210?

Thanks a lot.
int zipCode = 210;
               string strZipCode;
                strZipCode = "00" + zipCode.ToString();
                zipCode = Convert.ToInt32(strZipCode);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
why not keep it a string?

Author

Commented:
because this code is encapsulated withing some existing static method that was written by someone so I don't want to change the return type from int to string, e.g.:

public static int GetZip(int zipCode)
{
               //int zipCode = 210;
               string strZipCode;
                strZipCode = "00" + zipCode.ToString();
                zipCode = Convert.ToInt32(strZipCode);
                 return zipCode;
}

Author

Commented:
you know what? I'm just going to change it to string. It was a bad design by that someone in the first place anyway.
Thanks.
i think that is your best bet -- i know of no other way to get leading 0's to stick.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial