Re: Parse MultiCurrency Strings to Decimals...

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Nicholas,

Thanks for your response. Your point is very well taken regarding how I
would expect .NET to know... but I was hoping that I could somehow set
the culture to ALL. Meaning that, the .NET framework would parse the
numeric string regardless -- given any acceptable currency symbol.

If this is a poor assumption, it appears that my only choice is to
iterate through the characters of the string and only pull out the
numeric values. (With distinction to negative values, comma separated
thousands, etc). I came up with the following function:

public static decimal StripCurrencySymbol(string number)
{
string temp = null;
decimal newNumber = 0;
try
{
// if number is negative using parenthesis
if (number.IndexOf("(") == 0 || number.IndexOf(")") > 0)
{
number = number.Replace("(", "");
number = number.Replace(")", "");
number = "-" + number;
}

for (int i = 0; i < number.Length; i++)
{
int c = (int) number[i];
if( c > 47 && c < 58 || // 0 through 9
c == 46 || // .
c == 45 ) // -
{
temp += ((char) c).ToString();
}
}
newNumber = Decimal.Parse(temp);
}
catch
{
throw new ArgumentOutOfRangeException("...");
}
return newNumber;
}

What do you think?

[By the way, I did some digging and I realized you've been answering my
questions since circa 2002!]

Thanks,
-- Navid

.