hi every one

i need your help to now if there is a way in c# can write int numbers like this
4 -> 0004 or

3 -> 0003 or

25 -> 0025 or

123 -> 0123 or

1234 -> 1234 (no change)

i want to generate ID that ID should cotain four digites how can i do that

plz help me

Best Regards,

## All 8 Replies

for retriving:
SELECT RIGHT('000' + CONVERT(VARCHAR(3),codigo),3) FROM Documentos

inserting
use zeros + ur value

insert - may b it wil work

? i gave for select query, samething try for insert query. u wil get result

u can use string format

``````int IDNum=25;
string ID;
ID=IDNum.ToString("0000");``````

Hi,

Try this

``````int id = 2;
string idStr;

Regards,
Camilo

This MSDN article about Custom Numeric Strings has good examples

easiest way:

``````int myInt = 5;
string formattedInt = myInt.toString("0000");
int readFormatted = int.Parse(formattedInt); //in case you want it back to an integer``````

easiest way:

``````int myInt = 5;
string formattedInt = myInt.toString("0000");
int readFormatted = int.Parse(formattedInt); //in case you want it back to an integer``````

Sure but how do you explain getting "1234 -> 1234" or "123 -> 0123"? That is not a solution, but just hard coding all your requirements. That we could have done from the beginning, =_=; What we want is a program that will check the entered integers, if the value is a 4 digit, i.e. greater than 999, then no need to edit the value, however if it is less than that you need to check between 0-99 and 100-999 and accordingly append the zeros at the beginning.

well based on the original post, using ToString("0000") works perfectly since it appends 0's if the number is less than 4 digits (less than 999) and does nothing if the number has 4 digits or more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.