I have a value "0001"
as string
.
So, how to insert the value to field with type data int
?
I work in c#
example :
string val = "0001";
int getVal = val;
I have a value "0001"
as string
.
So, how to insert the value to field with type data int
?
I work in c#
example :
string val = "0001";
int getVal = val;
You can use Int32.Parse
or Int32.TryParse
methods. But remember, 0001
is just a textual representation of a number... i.e. a string
. When you parse this string, you will get 1
not 0001
. There is nothing 0001
as an int
.
string val = "0001";
int getval;
if (Int32.TryParse(val, out getval))
{
//Successfull parsing
}
else
{
//Your string is not a valid integer.
}
Difference between these methods is; Int32.Parse
throws FormatException
if your string is not a valid Int32
. But Int32.TryParse
doesn't throw any exception in any case. It just returns false
if parsing is not succeed.
You can convert a string to an int by using int.Parse()
or int.TryParse()
which won't throw an exception if the parsing fails.
However be aware that 0001
and 1
are represented in the exact same way when converted to integer. If you require the trailing zeroes, then you have to retain the string and convert to an int only when you need to do calculations.
int getVal;
string val = "0001";
Int.tryparse(val,out int getVal);
For more information http://www.codeproject.com/Articles/32885/Difference-Between-Int-Parse-Convert-ToInt-and is a tutorial
tell me any number that is 0001 and not 1? they are the same. It depends on how you print the number or store the number back in a string.