How can i convert English digits to Arabic digits?

前端 未结 7 784
有刺的猬
有刺的猬 2020-11-27 20:59

I have this C# code for example

DateTime.Now.ToString(\"MMMM dd, yyyy\");

Now the current thread is loading the Arabic culture. So the resu

7条回答
  •  失恋的感觉
    2020-11-27 21:38

    I've taken @Marcel B approach/workaround as I'm facing the same issue as the original question states.

    In my case, it is for "ar-KW" Culture. The only difference is that I'm using the NumberFormat.NativeDigits which is already part of the CultureInfo.

    You can check that like this (based on your current thread scenario):

    Thread.CurrentThread.CurrentCulture.NumberFormat.NativeDigits
    

    So, the code will look like this:

    private static class ArabicNumeralHelper
    {
        public static string ConvertNumerals(this string input)
        {
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    
            if (new string[] { "ar-lb", "ar-SA" }
                  .Contains(cultureInfo.Name))
            {
                return input.Replace('0', cultureInfo.NumberFormat.NativeDigits[0])
                            .Replace('1', cultureInfo.NumberFormat.NativeDigits[1])
                            .Replace('2', cultureInfo.NumberFormat.NativeDigits[2])
                            .Replace('3', cultureInfo.NumberFormat.NativeDigits[3])
                            .Replace('4', cultureInfo.NumberFormat.NativeDigits[4])
                            .Replace('5', cultureInfo.NumberFormat.NativeDigits[5])
                            .Replace('6', cultureInfo.NumberFormat.NativeDigits[6])
                            .Replace('7', cultureInfo.NumberFormat.NativeDigits[7])
                            .Replace('8', cultureInfo.NumberFormat.NativeDigits[8])
                            .Replace('9', cultureInfo.NumberFormat.NativeDigits[9]);
            }
            else return input;
        }
    }
    

    I hope it helps.

提交回复
热议问题