Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - How can i convert English digits to Arabic digits?

I have this C# code for example

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

Now the current thread is loading the Arabic culture. So the result is like this

???? 19, 2010

But i don't want the '2010' and the '19' to be in English (also known as Latin or West Arabic digits) - I want East Arabic numbers like "?".

I tried

DateTime.Now.ToString("MMMM dd, yyyy", CultureInfo.GetCultureInfo("ar-lb"));

gave the same result. So any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thy this workaround (just list all cultures you want to use this numerals in the string array):

private static class ArabicNumeralHelper
{
    public static string ConvertNumerals(this string input)
    {
        if (new string[] { "ar-lb", "ar-SA" }
              .Contains(Thread.CurrentThread.CurrentCulture.Name))
        {
            return input.Replace('0', 'u06f0')
                    .Replace('1', 'u06f1')
                    .Replace('2', 'u06f2')
                    .Replace('3', 'u06f3')
                    .Replace('4', 'u06f4')
                    .Replace('5', 'u06f5')
                    .Replace('6', 'u06f6')
                    .Replace('7', 'u06f7')
                    .Replace('8', 'u06f8')
                    .Replace('9', 'u06f9');
        }
        else return input;
    }
}

Then use the method, for all of your strings you want to have 'central Arabic numerals' in, like this:

DateTime.Now.ToString().ConvertNumerals();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...