本文介绍了如何强制我的C#Windows窗体使用阿拉伯数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图迫使我的表格使用阿拉伯语格式,例如将(1-2-3-4-..,etc)数字显示为(٠,١,٢,٣,٤,٥,٦,٧,٨,٨,٩),无论它是 Textbox 还是 lables 或其他

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

,这是一个接受的答案,它说cultureinfo会帮不上忙吗?因为我认为答案不正确

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

所以请有人帮助我

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、2、3 ...)进行数学运算.因此,即使使用 ar-SA 区域性,您在格式化数字时也会得到这些.使用您自己的格式功能来获取东部阿拉伯数字.

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

示例:

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

输出:

但是, num.ToString(ArabicNumberFormatProvider.Instance)不起作用.有关详细信息,请参见 https://stackoverflow.com/a/6240052/880990 .

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

这篇关于如何强制我的C#Windows窗体使用阿拉伯数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:30