471,319 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Parse MultiCurrency Strings to Decimals...

What's the best way to parse a currency string to a decimal given the
possibility of multiple currencies? That is, my numeric string can be
($12.00) or -£12.00; in either case I want -12.00 to be returned. I
understand that this may be slightly difficult given non-symbol
currency strings (F or Kr) but I figured that the CultureInfo should be
able to take care of it somehow.

The closest solution I came up with, short of iterating through the
string and picking up each number, is the following:

public static decimal StripCurrencySymbol(string number)
{
CultureInfo culture = new CultureInfo("en-GB");
decimal newNumber;
try
{
newNumber = Decimal.Parse(number, NumberStyles.Any, culture);
}
catch
{
throw new Exception("...");
}
return newNumber;
}

The problem is, of course, that since the culture is set to UK, any
string with $ generates an exception. If I set the culture to US, any
string with £ will generate an exception. This problem continues with
the Euro symbol as well.

Is there any way to parse the string given "ANY" culture? I hope this
isn't something simple that I am missing. Thanks.

Dec 30 '05 #1
5 3528
Navid,

In this case, you can create an instance of the NumberFormatInfo class,
and set the CurrencySymbol to the symbol for the current currency. You can
also set the CurrencyNegativePattern and CurrencyPositivePattern properties
to indicate what the pattern should be.

Then, you pass this instance to the Parse method of Decimal, and it
should give you the result that you want.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Navid Azimi" <na*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
What's the best way to parse a currency string to a decimal given the
possibility of multiple currencies? That is, my numeric string can be
($12.00) or -£12.00; in either case I want -12.00 to be returned. I
understand that this may be slightly difficult given non-symbol
currency strings (F or Kr) but I figured that the CultureInfo should be
able to take care of it somehow.

The closest solution I came up with, short of iterating through the
string and picking up each number, is the following:

public static decimal StripCurrencySymbol(string number)
{
CultureInfo culture = new CultureInfo("en-GB");
decimal newNumber;
try
{
newNumber = Decimal.Parse(number, NumberStyles.Any, culture);
}
catch
{
throw new Exception("...");
}
return newNumber;
}

The problem is, of course, that since the culture is set to UK, any
string with $ generates an exception. If I set the culture to US, any
string with £ will generate an exception. This problem continues with
the Euro symbol as well.

Is there any way to parse the string given "ANY" culture? I hope this
isn't something simple that I am missing. Thanks.
Dec 31 '05 #2
Thanks for the prompt response Nicholas, but how will I know what the
CurrencySymbol for a given numeric string will be?

The assumption here is that I could get any string, may it be:

£12.00
-£43
(£5.99)
34.00
12.5
(0)
-$12
($56.34)

These are some sample strings which should be parsed correctly to a
decimal value. I would also like to include the Euro, F, Kr, etc as
well. The point is, I will be getting a currency value (with or without
a currency symbol) where I would like to extract only the sign and
value.

Dec 31 '05 #3
Navid,

Well, if you don't know what the currency symbol for any given string
is, then how do you expect .NET to know? What I mean is, there isn't any
obvious way to determine this. You have to aid the parse mechanism along.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Navid Azimi" <na*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Thanks for the prompt response Nicholas, but how will I know what the
CurrencySymbol for a given numeric string will be?

The assumption here is that I could get any string, may it be:

£12.00
-£43
(£5.99)
34.00
12.5
(0)
-$12
($56.34)

These are some sample strings which should be parsed correctly to a
decimal value. I would also like to include the Euro, F, Kr, etc as
well. The point is, I will be getting a currency value (with or without
a currency symbol) where I would like to extract only the sign and
value.
Jan 1 '06 #4
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

Jan 3 '06 #5
Navid,
In addition to the other comments.

I would consider using a RegEx or String.IndexOfAny to find & parse out the
currency symbol. Then based on the currency symbol lookup the specific
CultureInfo or NumericInfo class for that currency, which I then pass to
Decimal.Parse.

I would consider storing the currency symbol/culture pairs in a custom
configuration section in my app.config/web.config.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Navid Azimi" <na*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
What's the best way to parse a currency string to a decimal given the
possibility of multiple currencies? That is, my numeric string can be
($12.00) or -£12.00; in either case I want -12.00 to be returned. I
understand that this may be slightly difficult given non-symbol
currency strings (F or Kr) but I figured that the CultureInfo should be
able to take care of it somehow.

The closest solution I came up with, short of iterating through the
string and picking up each number, is the following:

public static decimal StripCurrencySymbol(string number)
{
CultureInfo culture = new CultureInfo("en-GB");
decimal newNumber;
try
{
newNumber = Decimal.Parse(number, NumberStyles.Any, culture);
}
catch
{
throw new Exception("...");
}
return newNumber;
}

The problem is, of course, that since the culture is set to UK, any
string with $ generates an exception. If I set the culture to US, any
string with £ will generate an exception. This problem continues with
the Euro symbol as well.

Is there any way to parse the string given "ANY" culture? I hope this
isn't something simple that I am missing. Thanks.
Jan 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tuang | last post: by
26 posts views Thread by Nige | last post: by
46 posts views Thread by vvk4 | last post: by
3 posts views Thread by Daniel Weinand | last post: by
3 posts views Thread by aaronwmail-usenet | last post: by
10 posts views Thread by Martin Eyles | last post: by
2 posts views Thread by franzdawg3 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.