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 3 24357
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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....
|
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...
|
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.
|
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",...
|
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...
|
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...
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |