Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
Convert.tostring() trims leftmost 0

ex.
int x= 01234
Convert.tostring(x)
results into 1234 and not 01234
Any suggestions
Thanks
Posted 14-Jan-13 21:08pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Integer values ignore all leading zeros: 01234 is teh same as 1234, is the same as 00000000000000000000000000001234
You need to format the ToString operation:
int x = 01234;
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString("D5"));
Will print:
1234
01234
  Permalink  
Comments
Marcus Kramer at 15-Jan-13 9:50am
   
+5. This is the correct answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
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!
Smile | :)
  Permalink  
Comments
Aarti Meswania at 16-Jan-13 0:12am
   
what is reason for downvote?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use TrimStart(). So when you converted it to string
string _YourText = "01234";
_YourText = _YourText.TrimStart(new Char[] { '0' } );
--or--
_YourText = _YourText.TrimStart('0');
String.TrimStart Method In MSDN[^]

Good luck,
OI
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HI,

you can use like this:

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

Thanks
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,425
1 OriginalGriff 5,918
2 Peter Leow 2,514
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100