-How to assign 01 which should not get converted to 1 with Int32 in C# and gives 01 when referred.
Asked
Active
Viewed 432 times
-3
-
3You can't store leading zero's in a numeric type. – tkausl Mar 04 '19 at 10:09
-
1Welcome to Stack Overflow. Check the Stack Overflow's [help on asking questions](http://stackoverflow.com/help/asking) first, please. Focus on [What topics can I ask about here](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask), [How to ask a good question](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – David Ferenczy Rogožan Mar 04 '19 at 10:09
-
leading zeros are dropped in numbers, you can force the format to show leading zeros, but 1 and 01 are stored exactly the same – BugFinder Mar 04 '19 at 10:10
2 Answers
1
That is not possible. "01" is a string representation of some sort, not an actual value of an integer with value "1". Data types in .NET usually don't have formatting.
Using ToString
you can format an integer to the format you require. If that is not what you want, you should use string
.

Patrick Hofman
- 153,850
- 22
- 249
- 325
0
In such case you don't need an integer but string instead:
string x = "01"
Unless what you need is a binary number:
in x = 0b01
Another option would be to store the value in an integer variable and format it with left-padded zero just before you need it:
int x = 1;
string formatted = x.ToString("D2");

David Ferenczy Rogožan
- 23,966
- 9
- 79
- 68