Solved

Currency string convertion

Posted on 2004-04-21
7
239 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:glsac
ID: 10878261
0
 
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

Industry Leaders: 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!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

756 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