code for generating numbers

Posted by Amritha444 under C# on 2/26/2011 | Points: 10 | Views : 1071 | Status : [Member] | Replies : 7
Hi all

I want to generate auto number corresponding to primary keys

like

if pk is 1 the no will be 000001

if pk is 10 :000010

if 100: 000100

the total digits in a number should be same..how to code for it in c#?


Thanks in advance




Responses

Posted by: Naimishforu on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You want C# logic for the same???

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amritha444 on: 2/26/2011 [Member] Starter | Points: 25

Up
0
Down
yes

I f have any existing code .please paste here

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
int num = 10;
Console.WriteLine(Convert.ToString(num).PadLeft(6,'0'));

Try this it works

Thanks and Regards
Madhu

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Haha...I was thinking similar solution...but this one looks great!!

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
I read your mind Naimish ....

Thanks and Regards
Madhu

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hehehehe!!!!

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/26/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
One more way too you can use that is as below

int yourNumber= 100;
Console.WriteLine(yourNumber.ToString("D6", CultureInfo.InvariantCulture));



Thanks and Regards
Madhu

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response