2

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()
    });
 }
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Ahmad
  • 89
  • 2
  • 15
  • 1
    For the record, The 196... are **Arabic** numbers while the ١٩٦... are **Indian** numbers. –  Oct 09 '19 at 11:10

3 Answers3

5

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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 3
    Why insert a test for having single-`char` digits when the code could just as easily be written to handle the case where they're not (and be easier to understand to boot) by indexing into the array? – Jeroen Mostert Oct 09 '19 at 10:32
  • @Jeroen Mostert: Thank you! I see; extracting single `zero` character is not the best approach – Dmitry Bychenko Oct 09 '19 at 10:43
  • Where does the `ci` variable come from? I guess it stands for `CultureInfo` so that is your `culture` variable isn't it? – Arthur Rey Oct 09 '19 at 10:47
  • Another possible improvement is to use `CultureInfo.InvariantCulture` if the culture in question has no digits; `CultureInfo.InvariantCulture.NumberFormat.NativeDigits` is exactly the array you'd expect it to be. – Jeroen Mostert Oct 09 '19 at 10:47
  • @Jeroen Mostert: Thank you! `CultureInfo.InvariantCulture.NumberFormat.NativeDigits` is more readable – Dmitry Bychenko Oct 09 '19 at 10:49
1

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;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

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

Walter Verhoeven
  • 3,867
  • 27
  • 36