473,372 Members | 991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 3650
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tuang | last post by:
I've been looking all over in the docs, but I can't figure out how you're *supposed* to parse formatted strings into numbers (and other data types, for that matter) in Python. In C#, you can say...
26
by: Nige | last post by:
I'm a complete novice to JS. I want to insert the date and time into a document in the format: WB-MMDDHHmm Where: WB- is a fixed string prefix (the whole string is a reference number) MM...
1
by: js | last post by:
I am trying to build a query on a SQL2000 text field which contains XML string. The query is like "select requestnumber from history where requestnumber is like '%re1%'". As you can see in the...
46
by: vvk4 | last post by:
I have an excel spreadsheet that I need to parse. I was thinking of saving this as a CSV file. And then reading the file using C. The actual in EXCEL looks like: a,b a"b a","b a,",b In CSV...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
3
by: aaronwmail-usenet | last post by:
Ok, I've seen many examples of javascript parsing an XML document retrieved from a file indicated by a URL. The problem is I need to parse (usually small) xml *strings* embedded in the...
10
by: Martin Eyles | last post by:
Hi, I am getting a string passed back from a web form in the format name1:variable1;name2:variable2... I would like to split this up into several variable, such that the variable names are...
3
by: starke1120 | last post by:
I have a table (tblVAS) that contains a field called VAS (Value Added Servies). This field is imported from an AS/400 and the filed contains several codes that relate to Value Added Services. The...
2
by: franzdawg3 | last post by:
I find it hard to believe that there is not a native solution to this problem built into VB.NET but based on what I've come up with from MSDN and google if there is one it is not obvious. I have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.