2

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;
}
sam
  • 2,493
  • 6
  • 38
  • 73

1 Answers1

3

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • I need to call it on each textbox to show the arabic numbers? and I am getting error `; expected` on that line `public object GetFormat(Type formatType) => this;` what the advantage of that line please ? `private ArabicNumberFormatProvider() { }` – sam Aug 11 '18 at 19:19
  • This is C#7 syntax. Changed it to old syntax. `private ArabicNumberFormatProvider() { }` makes the constructor private, so that you have to use `ArabicNumberFormatProvider.Instance` instead of `new ArabicNumberFormatProvider()`. It makes the class a [singleton](https://en.wikipedia.org/wiki/Singleton_pattern). – Olivier Jacot-Descombes Aug 11 '18 at 19:25