Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
LolaRun Avatar asked Feb 19 '10 17:02

LolaRun


People also ask

How do I change a number format to Arabic in Word?

Open an Office program file, such as a Word document. On the File tab, choose Options > Language. In the Set the Office Language Preferences dialog box, in the Editing Language list, choose the Arabic dialect you want, and then choose Add.

Are English numbers Arabic numbers?

The numbers English speakers use every day, known as Arabic numerals, were developed in the Maghreb during the 10th century. They made their way into Europe through Arab scholars in Al-Andalus (modern-day Andalusia in Spain), hence they are called Arabic numerals.

How do you write numbers in Arabic numerals?

Arabic numbering rules Digits from zero to nine are specific words, namely sifr (صِفْرٌ) [0], wahid (وَاحِدٌ) [1], ithnan (اِثْنَانِ) [2], thalatha (ثَلَاثَةٌ) [3], arba'a (أَرْبَعٌ) [4], khamsa (خَمْسَةٌ) [5], sitta (سِتَّةٌ) [6], sab'a (سَبْعَةٌ) [7], thamaniya (ثَمَانِيَةٌ) [8], and tis'a (تِسْعَةٌ) [9].

How can I type Arabic numbers on my keyboard?

Go to Tools > Options > click on the "Complex scripts" tab, then under General: Numeral select "Context". That way, numbers will appear Hindi (i.e. Arabic) when you're writing Arabic and Arabic (i.e. English) when you're writing English (as you probably know these numbers "1,2,3" are called Arabic numerals).


1 Answers

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.

like image 97
Sebastian Inones Avatar answered Sep 21 '22 11:09

Sebastian Inones