Solved

Currency string convertion

Posted on 2004-04-21
7
242 Views
Last Modified: 2012-05-04
If I convert a decimal representing a money sum to a string, I preferably do it using a sum.ToString("C"). The result is something like "$123.45" or "123,45 kr", depending on your regional settings in Windows (I guess!?).

How do I convert back from the string to a decimal/float/similar? Is there a function that trims the string dependant of the regional settings?
0
Comment
Question by:IRQ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10878372
If that doesn't work you may need to look at the specifics in the NumberFormatInfo.CurrentInfo object like below

string sep = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;

and the currency symbol

string symbal = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol;

there are some more options in there that you can then use to convert the currency string back to the a float.
                  
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10878420
Here is a very basic example

// replace the seperator either . or , or something else with a . and also replce the currency symbol with nothing
// affectively deleting it.
string sep = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
string sym = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol;
string total = txtTotal.Text;

total = total.Replace(sep,".");
total = total.Replace(sym,"");

MessageBox.Show(total);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:IRQ
ID: 10878625
Thanks guys, I made it myself by:
myDecimal = decimal.Parse(myString, NumberStyles.Currency, NumberFormatInfo.CurrentInfo);

Was that the same solution as yours, J?
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10879034
same idea, but yours is cleaner!
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11756995
PAQed, with points refunded (500)
ee_ai_construct - CS Mod
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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