23

I work on a project in C# which requires to use arabic numbers, but then it must store as integer in database, I need a solution to convert arabic numbers into int in C#. Any solution or help please? thanks in advance


From comments:

I have arabic numbers like ١،٢،٣،٤... and must convert to 1,2,3, or ٢٣٤ convert to 234

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shaahin
  • 1,195
  • 3
  • 14
  • 22

8 Answers8

31

Updated: You can use StringBuilder for memory optimization.

private static string ToEnglishNumbers(string input)
    {
        StringBuilder sbEnglishNumbers = new StringBuilder(string.Empty);

        for (int i = 0; i < input.Length; i++)
        {
            if (char.IsDigit(input[i]))
            {
                sbEnglishNumbers.Append(char.GetNumericValue(input, i));
            }
            else
            {
                sbEnglishNumbers.Append(input[i].ToString());
            }
        }

        return sbEnglishNumbers.ToString();
    }

Original Answer: use this Method

private string toEnglishNumber(string input)
{
    string EnglishNumbers = "";

    for (int i = 0; i < input.Length; i++)
    {
        if (Char.IsDigit(input[i]))
        {
            EnglishNumbers += char.GetNumericValue(input, i);
        }
        else
        {
            EnglishNumbers += input[i].ToString();
        }           
    }
    return EnglishNumbers;
}
Mohammed Osman
  • 3,688
  • 2
  • 27
  • 25
  • 4
    Really liked this because you can translate any `string` even if it's not "all digit" like dates, etc. – Ehsan88 Sep 08 '15 at 08:23
10

Unfortunately it is not yet possible to parse the complete string representation by passing in an appropriate IFormatProvider(maybe in the upcoming versions). However, the char type has a GetNumericValue method which converts any numeric Unicode character to a double. For example:

double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2

You could use it to convert one digit at a time.

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
9

Arabic digits like ١،٢،٣،٤ in unicode are encoded as characters in the range 1632 to 1641. Subtract the unicode for arabic zero (1632) from the unicode value of each arabic digit character to get their digital values. Multiply each digital value with its place value and sum the results to get the integer.

Alternatively use Regex.Replace to convert the string with Arabic digits into a string with decimal digits, then use Int.Parse to convert the result into an integer.

Terje Norderhaug
  • 3,649
  • 22
  • 25
  • I would guess that int.Parse can handle the Arabic code points without special treatment. I don't have a C# compiler handy to check it, however. I am fairly sure that the char.IsNumeric test returns true for these chars. Did you check that one of your suggested techniques is necessary here? – phoog May 04 '11 at 07:04
  • Yes, Tarje I think your solution is correct, I can extract value of arabic digits for example value for 4 (٤) is 1636, but right now I have problem to convert this value to in , here is my code : `if (char.IsDigit(c)) { bytes[1] = Convert.ToByte( char.GetNumericValue(c)); utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0); convertedChars.Append(convertedChar[0]); } ` in the code here : utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0); says buffer is too small or large. – Shaahin May 04 '11 at 07:10
  • Dear all, thanks for help: I found other solution, I can use Switch instead conversation. thanks again – Shaahin May 04 '11 at 07:46
  • 1
    If you are going to use any of suggested methods remember that there are two ranges for Arabic numbers (Actually one for persian) in Unicode! http://stackoverflow.com/questions/1676460/in-unicode-why-are-there-two-representations-for-the-arabic-digits – Ali Jul 24 '16 at 20:35
9

A simple way to convert Arabic numbers into integer

    string EnglishNumbers="";
    for (int i = 0; i < arabicnumbers.Length; i++)
    {
        EnglishNumbers += char.GetNumericValue(arabicnumbers, i);
    }
    int convertednumber=Convert.ToInt32(EnglishNumbers);
Mahmood Dehghan
  • 7,761
  • 5
  • 54
  • 71
user484458
  • 168
  • 3
  • 11
2

This is my solution :

public static string arabicNumToEnglish(string input)
{
    String[] map={"٠","١","٢","٣","٤","٥","٦","٧","٨","٩"};
    
    for (int i = 0; i <= 9; i++)
    {
        input=input.Replace(map[i],i.ToString());
    }
    
    return input;
}
MSS
  • 3,520
  • 24
  • 29
0

I know this question is a bit old, however I faced similar case in one of my projects and passed by this question and decided to share my solution which did work perfectly for me, and hope it will serve others the same.

private string ConvertToWesternArbicNumerals(string input)
{
    var result = new StringBuilder(input.Length);

    foreach (char c in input.ToCharArray())
    {
        //Check if the characters is recognized as UNICODE numeric value if yes
        if (char.IsNumber(c))
        {
            // using char.GetNumericValue() convert numeric Unicode to a double-precision 
            // floating point number (returns the numeric value of the passed char)
            // apend to final string holder
            result.Append(char.GetNumericValue(c));
        }
        else
        {
            // apend non numeric chars to recreate the orignal string with the converted numbers
            result.Append(c);
        }
    }

    return result.ToString();
}

now you can simply call the function to return the western Arabic numerals.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
0

to get the value of a digit, substract the zero character from it, e.g in normal numeric, '1'-'0' = 1, '2'-'0' = 2. etc.

For multidigit number you can use something like this

 result =0;
 foreach(char digit in number)
 {
     result *= 10; //shift the digit, multiply by ten for each shift
     result += (digit - '0)'; //add the int value of the current digit.
 }

just replace the '0' with the arabic zero if your number uses Arabic character. This works for any numeric symbols, as long as 0-9 in that symbol system are encoded consecutively.

Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
0

try this extension:

 public static class Extension
    {
        public static string ToEnglishNumbers(this string s)
        {
            return s.Replace("۰", "0").Replace("۱", "1").Replace("۲", "2").Replace("۳", "3").Replace("۴", "4")
                .Replace("۵", "5").Replace("۶", "6").Replace("۷", "7").Replace("۸", "8").Replace("۹", "9");
        }

        public static int ToNumber(this string s)
        {
            if (int.TryParse(s.ToEnglishNumbers(), out var result))
            {
                return result;
            }

            return -1;
        }

        public static string ToArabicNumbers(this string s)
        {
            return s.Replace("0", "۰").Replace("1", "۱").Replace("2", "۲").Replace("3", "۳").Replace("4", "۴")
                .Replace("5", "۵").Replace("6", "۶").Replace("7", "۷").Replace("8", "۸").Replace("9", "۹");
        }
    }
foad abdollahi
  • 1,733
  • 14
  • 32