using Silverlight I am having trouble with the following code:
CultureInfo culture = new CultureInfo("ar-EG");
CultureInfo invCulture = CultureInfo.InvariantCulture;
Result.Text = String.Format(culture.NumberFormat, "{0:C}", 70000000.00);
// Does Not Work
//Result2.Text = String.Format(invCulture.NumberFormat, "{0}", double.Parse(Result.Text, invCulture));
// Does Not Work
//Result2.Text = String.Format(culture.NumberFormat, "{0}", double.Parse(Result.Text, culture.NumberFormat));
// Does Not Work
//Result2.Text = Convert.ToString(Decimal.Parse(Result.Text.Replace(" ", ""), NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowTrailingWhite));
Ultimately I’d like prove that I can transform a number to the Egyptian (Arabic) currency formatted string and then get my original number again.
Any time I try to convert my number back to the original double or decimal I receive an FormatException error ‘Input string was not in a correct format.’.
Any help please?
You won’t be able to convert the output string back to a number using
double.Parseas that expects the input to be purely numeric.If you want the value to go backwards and forwards from a number to a formatted string put the formatting in the XAML:
Source
Given that you’re not in XAML you’ll need to strip the currency symbol off the text first. You can get the currency symbol from the
RegionInfo.CurrencySymbolProperty as well asculture.NumberFormat.CurrencySymboland then remove that from the formatted text before passing todouble.Parse.As Mr Young points out in his comment, there is an overload of
Decimal.Parsethat takes aStringandIFormatProviderwhich provide additional information about the string – such as the fact it contains a currency symbol.