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

和自甴很熟 提交于 2021-02-10 06:31:41

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/51802254/how-to-force-my-c-sharp-windows-forms-to-use-arabic-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!