Ahmad
Ahmad

Reputation: 141

How to convert an English number to an Arabic number e.g., 196 to ١٩٦?

Does anyone know as to how we can convert an English number like 196 to its Arabic form like ١٩٦ in .Net CORE C#.

foreach (DataRow dr in ds.Tables[0].Rows)
{
    lstSurahs.Add(new Quran
    {
        ID         = Convert.ToInt32(dr["ID"].ToString()),
        DatabaseID = Convert.ToInt32(dr["DatabaseID"].ToString()),
        SuraID     = Convert.ToInt32(dr["SuraID"].ToString()),

        // Need Arabic Form
        VerseID    = Convert.ToInt32(dr["VerseID"].ToString().ConvertNumerals()), 

        AyahText   = dr["AyahText"].ToString()
    });
 }

Upvotes: 3

Views: 1620

Answers (3)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

There's no such method, but we can implement it; let's put the task as general as we can:

Given a string source and CultureInfo culture, turn all digits within source into national digits if culture provides them

Code:

  using System.Globalization;
  using System.Linq;

  ...

  public static partial class StringExtensions {
    public static String ConvertNumerals(this string source, 
                                         CultureInfo culture = null) {
      if (null == source)
        return null;

      if (null == culture)
        culture = CultureInfo.CurrentCulture;

      string[] digits = culture.NumberFormat.NativeDigits.Length >= 10 
        ? culture.NumberFormat.NativeDigits
        : CultureInfo.InvariantCulture.NumberFormat.NativeDigits;

      return string.Concat(source
        .Select(c => char.IsDigit(c)
           ? digits[(int) (char.GetNumericValue(c) + 0.5)]
           : c.ToString()));
    }
  } 

Demo:

  // "ar-SA" is "arabic Saudi Arabia"
  Console.WriteLine("test 123".ConvertNumerals(CultureInfo.GetCultureInfo("ar-SA")));
  // "en-US" is "english United States"
  Console.WriteLine("test 123".ConvertNumerals(CultureInfo.GetCultureInfo("en-US"))); 

Outcome:

test ١٢٣
test 123

Upvotes: 6

Salah Akbari
Salah Akbari

Reputation: 39966

The following method should works as you wanted:

private string toArabicNumber(string input)
{
    var arabic = new string[10] { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
    for (int j = 0; j < arabic.Length; j++)
    {
        input = input.Replace(j.ToString(), arabic[j]);
    }
    return input;
}

Or another solution:

private string ConvertNumber(string englishNumber)
{
    string theResult = "";
    foreach (char ch in englishNumber)
    {
        theResult += (char)(1776 + char.GetNumericValue(ch));
    }
    return theResult;
}

Upvotes: 1

Walter Verhoeven
Walter Verhoeven

Reputation: 4421

Perhaps have a look at Globalization in .net, set the language of the exe to Arabic (by default) or when starting and it will probably start working all by it self.

To force something in Arabic you can use: yourNumber.ToString("N2", CultureInfo.GetCultureInfo("ar-SA"));

have a look at https://dotnetfiddle.net/e1BX7M

Upvotes: 0

Related Questions