13,287,856 members (37,411 online)
Rate this:
See more:
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:

## 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```
Marcus Kramer 15-Jan-13 9:50am

+5. This is the correct answer.
Rate this:

## 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!
:)
Aarti Meswania 16-Jan-13 0:12am

what is reason for downvote?
Rate this:

## 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
v2
Rate this:

## Solution 1

HI,

you can use like this:

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

Thanks

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

Top Experts
Last 24hrsThis month
 ppolymorphe 120 CPallini 60 Dave Kreskowiak 50 OriginalGriff 40 thatraja 40
 OriginalGriff 1,653 Richard MacCutchan 880 ppolymorphe 764 CPallini 720 Jochen Arndt 560

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 15 Jan 2013