parsing dollar values - URGENT

Hi,

I have a string = to a dollar value. eg:

string dollars = "$15.45";

how do I parse it into a decimal?

i want:

decimal decPrice == 15.45;

but i want to do it by parsing the string into a decimal.. do I use an Iformatprovider or something? please give an example, i don't know how to use iformatprovider

thanks

-Trevor
LVL 8
trevorhartmanAsked:
Who is Participating?
 
tusharashahConnect With a Mentor Commented:
Hi Trevor,

Try this:
-------------------------------------------------------------------------------------------------------------------------------------
string dollars = "$15.45";
double dDollars;  
bool parse = Double.TryParse("$15.45", NumberStyles.Currency, NumberFormatInfo.CurrentInfo, out dDollars);
-------------------------------------------------------------------------------------------------------------------------------------

-tushar
0
 
tusharashahConnect With a Mentor Commented:
One simple thing you can do is, befor parsing it to Decimal Replace $ with emptystring:

string dollars = "$15.45";
dollars = dollars.Replace("$",""); // new value of dollars will be 15.45 now


-tushar
0
 
trevorhartmanAuthor Commented:
thanks for the reply, i'll put that in place for now... but i'm curious to see how to do it with an IFormatProvider because I want to be able to use that for other things in the future.  How do I tell decimal.Parse that my string is in a currency format so it knows how to parse?

-Trevor
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
msdixonConnect With a Mentor Commented:
and here's how to get it into a decimal value type


string dollars = "$15.45";
decimal amt = decimal.Parse(dollars.Replace("$", ""));
0
 
AerosSagaConnect With a Mentor Commented:
Dim delim As String = "$"
dollars.trim(delim.ToCharArray())

Aeros
0
 
trevorhartmanAuthor Commented:
thx for the suggestions guys, but i'm still wondering on the IFormatProvider thing...
0
 
trevorhartmanAuthor Commented:
Thanks tushar.

this did it for me:

double dollars = double.Parse("$13.40", NumberStyles.Currency);

-Trevor
0
 
tusharashahCommented:
Nice to have you going!

Thanks for A

-tushar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.