开发者

Having trouble transforming an ar-EG currency string back to a normal number in Silverlight

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.Al开发者_运维问答lowCurrencySymbol | 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.Parse as 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:

<TextBlock Text={Binding NumericValue, StringFormat=c, Mode=TwoWay} />

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.CurrencySymbol Property as well as culture.NumberFormat.CurrencySymbol and then remove that from the formatted text before passing to double.Parse.

As Mr Young points out in his comment, there is an overload of Decimal.Parse that takes a String and IFormatProvider which provide additional information about the string - such as the fact it contains a currency symbol.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜