Let's assume the function will always return valid number, so we are not going to check for errors or special cases. Let's also assume that the function returns integer, so I'll do all my code samples for integer. If it's not integer then the code provided below can be easily changed to work with the other types.
If you can guarantee that NumberFormatInfo for current culture in the code where you parse the string will always have "," sign as NumberGroupSeparator then the easiest way to parse the result of GetCurrentPrice() function will be:
int.Parse(svalue, NumberStyles.AllowThousands);
If you cannot guarantee that NumberFormatInfo for the current culture in the code where you parse the string will always have "," sign as NumberGroupSeparator then I believe the easiest way to do the task is:
int.Parse(svalue, NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo);
This will work as NumberFormatInfo.InvariantInfo has "," as NumberGroupSeparator.
Also note that the function name is GetCurrentPrice, so if it will ever be changed to return currency values we can pretty easy modify the code to accommodate the change.
Replied on Mar 25 2011 3:23PM
.