How can i convert English digits to Arabic digits?
Asked Answered
M

8

13

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?

Mahan answered 19/2, 2010 at 17:4 Comment(1)
What do you expect to happen? en.wikipedia.org/wiki/Arabic_digitsRidotto
R
18

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();
Ridotto answered 19/2, 2010 at 18:17 Comment(1)
The Wikipedia article you link to says that the standard Arabic numerals are used in the Arab world and that the Eastern Arabic numerals you refer to are only used in a few specific nations.Culminate
C
6

As a quick test, I wrote this to list all the cutures which don't have "2010" in the year:

        foreach (var ci in 
            from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
            where !c.IsNeutralCulture
            let date = DateTime.Now.ToString("MMMM dd, yyyy", c)
            where !date.Contains("2010")
            orderby c.Name
            select new {c.Name, date})
        {
            Console.WriteLine("{0} : {1}", ci.Name, ci.date);
        }

the results are:

ar-SA : ربيع الأول 05, 1431
dv-MV : ربيع الأول 06, 1431
prs-AF : ربيع الأول 06, 1431
ps-AF : ربيع الأول 06, 1431
th-TH : กุมภาพันธ์ 19, 2553

To convert the numbers to Arabic text, it looks like this "NumToArabicString" project will do it. It doesn't look like there's anything built into the .net framework though.

Chantilly answered 19/2, 2010 at 17:54 Comment(0)
N
6

The only solution is to manually convert the digits.

You could use the CurrentCulture.NumberFormat.NativeDigits array instead of explicitly providing the Unicode characters to support any other languages in addition to the Arabic-Indic digits, but as far as builtin support by culture-aware classes such as DateTime it seems unimplemented or something.

Necessitate answered 19/5, 2010 at 16:51 Comment(0)
T
4

Try this:

public static string ToIndicDigits(this string input)
{
        return input.Replace('0', '\u0660')
                .Replace('1', '\u0661')
                .Replace('2', '\u0662')
                .Replace('3', '\u0663')
                .Replace('4', '\u0664')
                .Replace('5', '\u0665')
                .Replace('6', '\u0666')
                .Replace('7', '\u0667')
                .Replace('8', '\u0668')
                .Replace('9', '\u0669');
    }
}
Taboret answered 13/8, 2013 at 16:28 Comment(0)
B
4

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.

Barbet answered 2/10, 2019 at 10:36 Comment(0)
P
2

You can use the Windows.Globalization.NumberFormatting.NumeralSystemTranslator class to translate between the Latin and any of the supported numeral systems. To translate to Arabic, set the NumeralSystem property to "Arab", then you can call the TranslateNumerals method.

Alternatively you can just use Windows.Globalization.DateTimeFormatting.DateTimeFormatter class directly.

Pes answered 24/9, 2013 at 18:4 Comment(0)
P
0

This is also an option to convert number 35852:

string.Join("", 35852.ToString().Select(x => CultureInfo.GetCultureInfo("ar-lb").NumberFormat.NativeDigits[int.Parse(x.ToString())]));

Outpus:

٣٥٨٥٢ 
Portamento answered 2/9, 2021 at 10:49 Comment(1)
Please add further details to expand on your answer, such as working code or documentation citations.Speciation
C
-1

We use Arabic numerals in English. As such, what you're seeing is the correct - and only possible - behaviour.

Culminate answered 19/2, 2010 at 17:46 Comment(1)
The english numerals are called Arabic, but the arabs represent them differently as such 0-9: ٠١٢٣٤٥٦٧٨٩Mahan

© 2022 - 2024 — McMap. All rights reserved.