426,060 Members | 1,878 Online
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

# converting odd number digits to even number

 P: n/a Hello Everyone, I am trying to convert the digits to even number so for example if I have 3 digit number, I want it to be 4 digit and If I have 5 digit number, I want it to be 6 digit. How can i do it in C# Below is what I am trying to di number = 123 I want it to be 0123 and 23456 I want it o be 023456 Thanks. Jun 29 '07 #1
4 Replies

 P: n/a Well, you can't do this to the "number", since the number itself doesn't care about things like leading zeros. Just treat it as a string... string sVal = value.ToString(); // or whatever if((sVal.Length % 2) == 1) { // odd length sVal = "0" + sVal; // left-pad with zero } That do? Marc Jun 29 '07 #2

 P: n/a On Fri, 29 Jun 2007 14:52:01 -0700, Vinki

 P: n/a string myNum = 123.ToString(); string fubar = ((myNum.Length % 2) == 1) ? "0" + myNum : myNum; Good luck with your assignment!! Regards John Timney (MVP) http://www.johntimney.com http://www.johntimney.com/blog "Vinki"

 P: n/a "Vinki"

### This discussion thread is closed

Replies have been disabled for this discussion.