473,383 Members | 1,803 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,383 software developers and data experts.

parse currency string to decimal

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
Nov 16 '05 #1
3 24341
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]
- mv*@spam.guard.caspershouse.com

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com
"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:un**************@tk2msftngp13.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: N | last post by:
Hi, I would like to parse out each value that is seperated by a comma in a field and use that value to join to another table. What would be the easiest way to do so without having to write a...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
3
by: Mark | last post by:
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...
1
by: Juan | last post by:
Best way to convert back a currency string into a number(int or long)? If I have, for example, $ 15.000,00 what is the best way to make it again an int or long? pls help!, Juan.
0
by: R. John Reed | last post by:
Hi All, I'm am looking to convert a currency string (e.g. "$1,234.56" to a double value). It appears this will work: double val = Convert.ToDouble(Double.Parse­("$123,456.78901",...
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
1
by: mdawoodk | last post by:
i am getting error "input string was not in correct format" when converting a string decimal into integer value. code is like this: string strVal = ""; int nVal = 0; strVal = "14.9"; nVal...
3
by: Mike Howarth | last post by:
Hi Seem to be having a bit of brainfreeze this evening. Basically I'm reducing an array of prices like so: This gives me a string of '86.00.00' which I am trying to use with decimal...
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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.