Answered Trouble in parsing AE currency price to double value

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
I have one function which parses the string text to double value and it works for almost all cultures except UAE

Code

Function:
public static double Parse(string text, string culture)
        {
              
                return double.Parse(text.Replace(" ", ""), NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
                   new CultureInfo(culture));
        }


Value for UAE which has issue: "د.إ. 259.00"

Culture code: "ar-AE"

Exception Getting: System.FormatException: 'Input string was not in a correct format.'
 
what?
 
Look back to post 6. Also read the SO thread.
 
Mea..

Working with Hebrew/Arabic doesn't serve enough hours in a day to go working out which symbols are LTR/RTL. Simplest solution would be to cut the symbol off and add it later.

It's not worth killing braincells for
 
No, OP's problem is that the currency symbol in input is incorrect.
Yes, I gathered that from the SO comments and answers. GIGO.

And so since it was a bad input issue, I figured I would start from scratch by going from decimal to a currency string and then back again. I started getting the same error as the OP which I didn't expect. I would have thought that MS would at least be able to round trip something that it generated. It took adding in the trailing whitespace flag to finally get it to round trip.
 
There are a lot of things Microsoft do that leaves me scratching my head Skydiver. Including breaking their own object orientation rules as well as reusable code not being used. Take that enum topic the other day as proof of bad designing principles. That was just dumb thinking, so yes, I agree with you when you said
I would have thought that MS would at least be able to round trip something that it generated.
Its sometimes to be expected. That's Microsoft for you.
 
Woohoo! I got it to work!
C#:
var culture = new CultureInfo("ar-AE");
var value = 259.00m;

var s = value.ToString("C", culture);
MessageBox.Show(s);

var flags = NumberStyles.AllowCurrencySymbol |
            NumberStyles.AllowDecimalPoint |
            NumberStyles.AllowThousands |
            NumberStyles.AllowLeadingWhite |
            NumberStyles.AllowTrailingWhite;
var v = decimal.Parse(s, flags, culture);
MessageBox.Show(v.ToString("C", culture));

The magic are in lines 10-11.
This has worked!!1 wohooo :)
 
I would have thought that MS would at least be able to round trip something that it generated. It took adding in the trailing whitespace flag to finally get it to round trip.
Leading/trailing white depends on culture, for my culture I had to add leading white. It's easier to just use the composite NumberStyles.Currency for currency strings:
C#:
var culture = new CultureInfo("ar-AE");
var value = 259.00m;
var s = value.ToString("C", culture);
value = decimal.Parse(s, NumberStyles.Currency, culture);
It won't work with broken input as in post 1 of course.
 
Back
Top Bottom