Re: Parse MultiCurrency Strings to Decimals...
- From: "Navid Azimi" <navid.azimi@xxxxxxxxx>
- Date: 3 Jan 2006 12:05:42 -0800
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
.
- References:
- Re: Parse MultiCurrency Strings to Decimals...
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Parse MultiCurrency Strings to Decimals...
- Prev by Date: Problem closing Word object from within C#
- Next by Date: Re: Regular Expression taking excessive CPU
- Previous by thread: Re: Parse MultiCurrency Strings to Decimals...
- Next by thread: Re: Parse MultiCurrency Strings to Decimals...
- Index(es):