IRQ
asked on
Currency string convertion
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?
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?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemConvertClassToDecimalTopic13.asp
If that doesn't work you may need to look at the specifics in the NumberFormatInfo.CurrentIn fo object like below
string sep = System.Globalization.Numbe rFormatInf o.CurrentI nfo.Curren cyDecimalS eparator;
and the currency symbol
string symbal = System.Globalization.Numbe rFormatInf o.CurrentI nfo.Curren cySymbol;
there are some more options in there that you can then use to convert the currency string back to the a float.
string sep = System.Globalization.Numbe
and the currency symbol
string symbal = System.Globalization.Numbe
there are some more options in there that you can then use to convert the currency string back to the a float.
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.Numbe rFormatInf o.CurrentI nfo.Curren cyDecimalS eparator;
string sym = System.Globalization.Numbe rFormatInf o.CurrentI nfo.Curren cySymbol;
string total = txtTotal.Text;
total = total.Replace(sep,".");
total = total.Replace(sym,"");
MessageBox.Show(total);
// 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.Numbe
string sym = System.Globalization.Numbe
string total = txtTotal.Text;
total = total.Replace(sep,".");
total = total.Replace(sym,"");
MessageBox.Show(total);
ASKER
Thanks guys, I made it myself by:
myDecimal = decimal.Parse(myString, NumberStyles.Currency, NumberFormatInfo.CurrentIn fo);
Was that the same solution as yours, J?
myDecimal = decimal.Parse(myString, NumberStyles.Currency, NumberFormatInfo.CurrentIn
Was that the same solution as yours, J?
same idea, but yours is cleaner!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.