Parsing numbers might fool you if you deal with software that must run under variing cultures.
The decimal separator is not the same on all cultures.
Depending on where you got your string from (e.g. is it localized or culture invariant?) then you must follow differing approaches.
If it is culture invariant, take one of the other suggested soulutions.
If localized, let the system parse the number, i.e. take the non-space text before the % symbol and pass it to the
TryParse
method.
E.g.
string s = "...";
var match = Regex.Match(s, @"^\s*(.*?)\s*%\s*$");
double percentage;
NumberStyles style = NumberStyles.Currency;
CultureInfo culture = CultureInf.CurrentCulture;
if (match.Success && double.TryParse(match.Groups[1].Value, style, culture, out percentage))
{
...
}
else
{
...
}
Cheers
Andi