Solved

parsing dollar values - URGENT

Posted on 2004-10-14
8
328 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:trevorhartman
8 Comments
 
LVL 18

Assisted Solution

by:tusharashah
tusharashah earned 350 total points
Comment Utility
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
 
LVL 8

Author Comment

by:trevorhartman
Comment Utility
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
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 75 total points
Comment Utility
and here's how to get it into a decimal value type


string dollars = "$15.45";
decimal amt = decimal.Parse(dollars.Replace("$", ""));
0
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 75 total points
Comment Utility
Dim delim As String = "$"
dollars.trim(delim.ToCharArray())

Aeros
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Author Comment

by:trevorhartman
Comment Utility
thx for the suggestions guys, but i'm still wondering on the IFormatProvider thing...
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 350 total points
Comment Utility
Hi Trevor,

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

-tushar
0
 
LVL 8

Author Comment

by:trevorhartman
Comment Utility
Thanks tushar.

this did it for me:

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

-Trevor
0
 
LVL 18

Expert Comment

by:tusharashah
Comment Utility
Nice to have you going!

Thanks for A

-tushar
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now