sam
sam

Reputation: 2596

How to Force my C# windows forms to use arabic number?

I am trying to force my forms to use arabic format like displaying (1-2-3-4-..,etc) numbers as (٠‎ , ١‎ , ٢‎ , ٣‎ , ٤‎ , ٥‎ , ٦‎ , ٧‎ , ٨‎ , ٩‎) in all areound my forms no matter if it is Textbox,orlablesor whatever it is

I searched and found a lout of question talking about this issue most of them not working and the other I believe are not seems to be good answer like the accepted answer here and as shown below what I have tried on form loading which has no effect on my issue

and here is an accepted answer that says that cultureinfo will not help is it right ?? because I do not think that answer is right

so please if someone can help me

private void F0102_Load(object sender, EventArgs e)
{
    CultureInfo ar_culture = new CultureInfo("ar-SA");
    Thread.CurrentThread.CurrentCulture = ar_culture;
    Thread.CurrentThread.CurrentUICulture = ar_culture;
}

Upvotes: 2

Views: 866

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

Most Arabic people use western Arabic numbers (1, 2, 3...) for math. Therefore, even with the ar-SA culture you get those when formatting numbers. Use your own formatting function to get eastern Arabic numbers.

public static string ToArabic(long num)
{
    const string _arabicDigits = "۰۱۲۳۴۵۶۷۸۹";
    return new string(num.ToString().Select(c => _arabicDigits[c - '0']).ToArray());
}

You can also create your own format provider:

public class ArabicNumberFormatProvider : IFormatProvider, ICustomFormatter
{
    public static readonly ArabicNumberFormatProvider Instance =
        new ArabicNumberFormatProvider();

    private ArabicNumberFormatProvider() { }

    public object GetFormat(Type formatType) { return this; }

    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        switch (arg) {
            case long l:
                return ToArabic(l);
            case int i:
                return ToArabic(i);
            default:
                return null;
        }
    }

    public static string ToArabic(long num)
    {
        const string _arabicDigits = "۰۱۲۳۴۵۶۷۸۹";
        return new string(num.ToString().Select(c => _arabicDigits[c - '0']).ToArray());
    }
}

Example:

string arabic = String.Format(ArabicNumberFormatProvider.Instance,"{0}", 1234567890);

Output:

۱۲۳۴۵۶۷۸۹۰

However, num.ToString(ArabicNumberFormatProvider.Instance) does not work. See https://stackoverflow.com/a/6240052/880990 for details.

Upvotes: 4

Related Questions