Shaahin
Shaahin

Reputation: 1225

How to convert Arabic number to int?

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

Upvotes: 23

Views: 11893

Answers (8)

Mohammad Asghari
Mohammad Asghari

Reputation: 426

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;
}

Upvotes: 33

MSS
MSS

Reputation: 3860

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;
}

Upvotes: 3

foad abdollahi
foad abdollahi

Reputation: 1988

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", "۹");
        }
    }

Upvotes: 0

Mohammed Melhem
Mohammed Melhem

Reputation: 1

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.

Upvotes: 0

user484458
user484458

Reputation: 168

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);

Upvotes: 9

Saeb Amini
Saeb Amini

Reputation: 24419

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.

Upvotes: 10

Louis Rhys
Louis Rhys

Reputation: 35637

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.

Upvotes: 0

Terje Norderhaug
Terje Norderhaug

Reputation: 3689

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.

Upvotes: 9

Related Questions