Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Convert.tostring() trims leftmost 0

ex.
int x= 01234
Convert.tostring(x)
results into 1234 and not 01234
Any suggestions
Thanks
Posted

integer variable will not consider Leading zeroes
so that
if you will assign
01234 to integer variable it will automatically save it in x variable as 1234

so, add leading zeros after converting x to string
C#
string myString = String.Format("{0:00000}",x) // this will add leading zeros in a way that passed number will be have minimum 5 digits e.g. 123 -> 00123

Happy coding!
:)
 
Share this answer
 
Comments
Aarti Meswania 16-Jan-13 0:12am    
what is reason for downvote?
Integer values ignore all leading zeros: 01234 is teh same as 1234, is the same as 00000000000000000000000000001234
You need to format the ToString operation:
C#
int x = 01234;
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString("D5"));
Will print:
1234
01234
 
Share this answer
 
Comments
fjdiewornncalwe 15-Jan-13 9:50am    
+5. This is the correct answer.
You can use TrimStart(). So when you converted it to string
C#
string _YourText = "01234";
_YourText = _YourText.TrimStart(new Char[] { '0' } );

--or--
C#
_YourText = _YourText.TrimStart('0');

String.TrimStart Method In MSDN[^]

Good luck,
OI
 
Share this answer
 
v2
HI,

you can use like this:

C#
String a = "000454332";
a=a.TrimStart('0');
int b = Convert.ToInt32(a);


Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900