Solved

Currency string convertion

Posted on 2004-04-21
7
225 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
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

13 Experts available now in Live!

Get 1:1 Help Now