Search code examples
c#entity-framework-4integer

How to store int with leading Zero in Database with EntityFramework?


I have integer a with "10115" and I able to convert that into string using below code.

int a = 10115;    
string sixd = a.ToString("D6");

and using this code I able to convert 10115 into 010115.

but when I convert 010115 into integer using below code but that always convert into 10115 so any trick to convert that into 010115 in int.

int b = Convert.ToInt32(010115); 

so any trick to store 010115 into integer ? and same in "010115" format not in 10115 if any one have idea so please help me.

And also I want to store same value in database "010115" in column where type is integer.


Solution

  • You can't have a leading zero for integers. It is only for their string representations.

    When leading zeros occupy the most significant digits of an integer, they could be left blank or omitted for the same numeric value. Therefore, the usual decimal notation of integers does not use leading zeros.

    That's why your Convert.ToInt32(010115); is equal to Convert.ToInt32(10115);

    and also i want to store same value in database "010115" in column where type is integer

    That's not possible. If you really save it as a 010115, you have to use some character type instead of integer. But I doubt that is a good idea.

    James Bond has a code number as 007 but even he can't save his code number to an integer column without loosing leading zero, how can we?