trying to figure out how to conver this zip string to an int. I get a cast exception:
member.Zip = reader.GetInt16(ordinals[(int)Enums.MemberColumn.Zip]);
UPDATE:
thanks all. Here's what I came up with that works for me good enough:
Int32.TryParse(reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]), out number) ? number : 0;
You need to get it as a string and then parse that string:
string zipString = reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]);
member.Zip = Int16.Parse(zipString);
DataReaders expect the underlying type of the field to be the same as the specific method you're calling. So GetInt16 requires an underlying 16 bit integer, GetBoolean requires an underlying bit, and GetString requires an underlying string. It won't do any conversion for you.
member.Zip = Convert.ToInt16(reader.GetString(ordinals[(int)Enums.MemberColumn.Zip]));
Related
As I saw in google, there are many implementation for string to an int or decimal... but not to a string.
For example:
decimal d1 = 32221.0210m;
Console.WriteLine(d1.ToString("#,###.00"));
This works but the problem is that I have a string and I don't want to cast it to a numeric.
string str = "32221.0210";
I want it to be:
str = "32,221.02";
Is it possible (without casting)?
For those who ask if is there any reason why not cast: Yes. I am using Infrastructures that receive a string. The infrastructures team don't want to convert it to decimal because they also may recieve another values such as date, etc...
In worse case, it is possible to TryParse decimal, so we know for sure it will work. But for now, we are trying to parse a string to a string as written above.
Thanks in advance :)
A temporary var of type decimal is required:
decimal d;
string str = decimal.TryParse("32221.0210", out d)? string.Format("{0:#,##0.00}", d):string.Empty;
I want to convert string number like "000023" to integer 000023 in RDLC report but when I am trying to convert it into integer it only displays 23 instead of 000023.
does anyone know how to do this ?
thanks
000023 is not an valid integer. When 000023 is converted to an integer, it will automatically be converted to the valid integer 23.
Jaq316's answer is correct.
How could you have an int as 000023? You could format it as a string with leading zeros which you have already it as a string.
But if you want to display 23 as a 000023 you can use String.PadLeft like;
public string Represent(string s)
{
int i = 0;
if (int.TryParse(s, out i))
{
return(i.ToString().PadLeft(6,'0'));
}
}
This method seems pointless of course since you have already 000023 in your database.
Integral types and Floating point types doesn't owns any formatting.
Formatting is done only when converting them to string. so you've to format it when you display it. There is no way to store an integer as 000023
static void Main(string[] args)
{
string str_val = "8584348,894";
//int prefix = Convert.ToInt32(str_val[0]); //prefix = 56 O_o
//int prefix = (int)str_val[0]; //what, again 56? i need 8!
int prefix = Convert.ToInt32("8"); //at least this works -_-
}
Any idea how to convert first symbol to right numeric value?
If you use:
Convert.ToInt32(str_val[0]);
then you are actually calling the overload:
Convert.ToInt32(char val);
which gives the Unicode/Ascii number of character being passed as a parameter.
If you want to convert first character, you need to force it to be a string type:
Convert.ToInt32(str_val.Substring(0, 1));
This way you call the overload:
Convert.ToInt32(string val);
which actually do what you want (convert string value to int value that this string represents).
Your trying to parse a string but passing in a char. Convert the character to a string first.
int prefix = Convert.ToInt32(str_val[0].ToString());
So the character value of 8 is the ASCII value 56, what you want to do is inteprete the value as a string rather than an ASCII Value. By using the .ToString() method you are converting the character into a null terminated string, which can be read by the ToInt32 method.
By doing this way Convert.ToInt32(str_val[0]) you are reading the character at a index of the string that converts the char to int. the int is the ascii for it
Like Bridge mentioned, it's getting the ASCII value. When you use the indexer on the string class to get just a character, it returns it as a char. If you read the char documentation you'll see that it is internally stored as a numeric UTF-16 value. It also has implicit conversions to and from most numeric types, which extract the UTF-16 value, or convert the numeric form into the character form. That's what you're doing.
What you mean to do is parse it as an int, not get the numeric representation of the UTF-16 value. That's where the Convert answers all come in.
Int32.Parse(str_val[0])
Will give you number in string.
dsgetQuesTypeID = objdalQuesTypeID.GetQuesTypeID(int.Parse(QuesTypeID)); //error here
Whats wrong ?
if you see the int.Parse in metadata explorer
//
// Summary:
// Converts the string representation of a number to its 32-bit signed integer
// equivalent.
//
// **Parameters:
// s:
// A string containing a number to convert.**
//
// Returns:
// A 32-bit signed integer equivalent to the number contained in s.
//
public static int Parse(string s);
it requires a string input to be parsed , but QuesTypeID is int so that's why no overloaded method is found so to correct the problem , you need to do following
int.Parse(QuesTypeID.ToString());
You have a member with the same name QuesTypeID as the name of the variable passed to the method. One is int and one is String. Try to rename the variable from the method to something else, like: quesTypeID. I think this would solve your problem. Also you should use Int.TryParse instead of Int.Parse.
You maybe passing null value into GetQuesTypeID(string QuesTypeID)
I'm trying to dynamically set an enum based on the value in a string so far so good I don't know what I've been doing wrong. I have the following code:
public enum TagLabels : long
{
TurnLeft = 0x0000000000000000000030A38DB1,
TurnRight = 0x00000000000000000000307346CC,
LiftApproach = 0x0000000000000000000012107A8D
}
TagLabels IDs;
string someID = "0x0000000000000000000012107A8D";
IDs = (TagLabels)Enum.Parse(typeof(TagLabels), someID ); //<== I get runtime error on this line
I cannot see what's wrong with what I'm doing.
Enum.Parse is intended to convert a string representation of the symbolic name into an enum val, as in Enum.Parse("TurnLeft"). If what you have is a string giving the numeric value, then you should just parse the string as the corresponding integer type and cast it to the Enum val.
IDs = (TagLabels)long.Parse("0x0000000000000000000012107A8D");
IDs = (TagLabels)Convert.ToInt64(someID, 16);
EDIT: You have a string that is in hex format and not a direct number. So, it needs conversion to int first.
If the Enum value exists, you can cast an int value to Enum type.
EDIT2: Changed after Marc's suggestion from Convert.ToInt32 to Convert.ToInt64
SomeID is a string and your enum is a long.
Try using TurnLeft instead of "0x0000000000000000000012107A8D"
Where is the string you're parsing? Aren't you trying to turn a string like "TurnLeft" into TagLabels.TurnLeft?
MSDN