parse currency string to decimal

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

How do you parse a currency string to a decimal? I'd like to avoid having
to parse the number out, removing the $ manually. That sounds like a hack.
There are times that this string will be currency and others when it will be
a text integer or decimal.

//This bombs because of the string having an improper format.
Decimal.Parse("$9,200.00")

Thanks in advance!
Mark
 
Mark,

You can do this by creating a NumberFormatInfo instance and setting the
properties on that to handle the particular aspects of your string. For
example, you want to set the CurrencySymbol property to "$", the
NumberDecimalDigits property to 2, NumberGroupSeparator to ",",
NumberGroupSizes to 3, etc, etc. Most of these are probably set by default,
but once you have that, you can pass it to the Parse method and it should
parse the value just fine.

Hope this helps.
 
Thanks Nicholas. This makes sense in theory. However, the code below bombs
with an exception message of "Input string was not in a correct format."
Suggestions? Thanks again.

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.CurrencySymbol = "$";
nfi.CurrencyDecimalSeparator = ".";
nfi.NumberGroupSeparator = ",";
nfi.NumberDecimalDigits = 2;
Response.Write("Parsed decimal = " + Decimal.Parse("$96,000.00",
nfi).ToString());

Thanks again!
Mark


Nicholas Paldino said:
Mark,

You can do this by creating a NumberFormatInfo instance and setting the
properties on that to handle the particular aspects of your string. For
example, you want to set the CurrencySymbol property to "$", the
NumberDecimalDigits property to 2, NumberGroupSeparator to ",",
NumberGroupSizes to 3, etc, etc. Most of these are probably set by default,
but once you have that, you can pass it to the Parse method and it should
parse the value just fine.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark said:
How do you parse a currency string to a decimal? I'd like to avoid having
to parse the number out, removing the $ manually. That sounds like a hack.
There are times that this string will be currency and others when it
will
be
a text integer or decimal.

//This bombs because of the string having an improper format.
Decimal.Parse("$9,200.00")

Thanks in advance!
Mark
 
Mark,

Actually, poking around, you could do this:

Double pdblValue = Double.Parse("$96,000.00", NumberStyles.Any);

It will allow the currency symbol, using the NumberFormatInfo returned
by the static CurrentInfo property on the NumberFormatInfo class.

Also, if you wanted to set the NumberFormatInfo instance up yourself, I
would clone the value returned by the static InvariantInfo property (because
the one returned by the property is read-only), and then set the
CurrencySymbol to "$". All the other defaults will give you what you want.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Mark said:
Thanks Nicholas. This makes sense in theory. However, the code below bombs
with an exception message of "Input string was not in a correct format."
Suggestions? Thanks again.

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.CurrencySymbol = "$";
nfi.CurrencyDecimalSeparator = ".";
nfi.NumberGroupSeparator = ",";
nfi.NumberDecimalDigits = 2;
Response.Write("Parsed decimal = " + Decimal.Parse("$96,000.00",
nfi).ToString());

Thanks again!
Mark


message news:%[email protected]...
Mark,

You can do this by creating a NumberFormatInfo instance and setting the
properties on that to handle the particular aspects of your string. For
example, you want to set the CurrencySymbol property to "$", the
NumberDecimalDigits property to 2, NumberGroupSeparator to ",",
NumberGroupSizes to 3, etc, etc. Most of these are probably set by default,
but once you have that, you can pass it to the Parse method and it should
parse the value just fine.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark said:
How do you parse a currency string to a decimal? I'd like to avoid having
to parse the number out, removing the $ manually. That sounds like a hack.
There are times that this string will be currency and others when it
will
be
a text integer or decimal.

//This bombs because of the string having an improper format.
Decimal.Parse("$9,200.00")

Thanks in advance!
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top