System.Globalization.NumberStyles

sureshcd10

Regular
Joined
Dec 25, 2003
Messages
77
See code below

string str = "APR 12.34% APR";
string aDecimal;
aDecimal = Decimal.Parse(str, System.Globalization.NumberStyles.Any).ToString();
Response.Write(aDecimal);

Here I am trying to retrieve the numeric part from the string str ? and is not working.

Is there any other idea to achieve the expected result

Thank u in advance
 
Parse methods

The Parse methods can only handle whitespace, not other characters. You therefore have to strip out all characters except numerics (and whitespace). You could do this with regular expressions, or using something like this:

C#:
//Untested code
int pos = str.IndexOfAny("0123456789".ToCharArray());
if (pos > -1) {
    str = str.Substring(pos);
    pos = str.LastIndexOfAny("0123456789".ToCharArray());
    str = str.Substring(0, pos + 1);

    aDecimal = Decimal.Parse(str, System.Globalization.NumberStyles.Any).ToString();
}

Note that this won't work if the string contains multiple numbers, but it should give you an idea of what needs to be done. I have to say that using the name aDecimal for a string variable is a bit strange.

Good luck :cool:
 
Back
Top