How to Convert Persian Digits in variable to Engli

2020-05-19 20:03发布

问题:

I want to change persian numbers which are saved in variable like this :

string Value="۱۰۳۶۷۵۱"; 

to

string Value="1036751";

How can I use easy way like culture info to do this please?

my sample code is:

List<string> NERKHCOlist = new List<string>();
NERKHCOlist = ScrappingFunction(NERKHCO, NERKHCOlist);
int NERKHCO_Price = int.Parse(NERKHCOlist[0]);//NERKHCOlist[0]=۱۰۳۶۷۵۱ 

<= So it can not Parsed it to int
And This is in my function which retun a list with persian digits inside list items

protected List<string> ScrappingFunction(string SiteAddress, List<string> NodesList)
{    
    string Price = "null";
    List<string> Targets = new List<string>();
    foreach (var path in NodesList)
    {
        HtmlNode node = document.DocumentNode.SelectSingleNode(path.ToString());//recognizing Target Node
        Price = node.InnerHtml;//put text of target node in variable(PERSIAN DIGITS)
        Targets.Add(Price);
    }
    return Targets;
}

回答1:

I suggest two approaches to handle this issue(I Create an extension method for each of them):

1.foreach and replace

public static class MyExtensions
{
     public static string PersianToEnglish(this string persianStr)
     {
            Dictionary<char, char> LettersDictionary = new Dictionary<char, char>
            {
                ['۰'] = '0',['۱'] = '1',['۲'] = '2',['۳'] = '3',['۴'] = '4',['۵'] = '5',['۶'] = '6',['۷'] = '7',['۸'] = '8',['۹'] = '9'
            };
            foreach (var item in persianStr)
            {
                persianStr = persianStr.Replace(item, LettersDictionary[item]);
            }
            return persianStr;
     }
}

2.Dictionary.Aggregate

public static class MyExtensions
{
      public static string PersianToEnglish(this string persianStr)
      {
            Dictionary<string, string> LettersDictionary = new Dictionary<string, string>
            {
                ["۰"] = "0",["۱"] = "1",["۲"] = "2",["۳"] = "3",["۴"] = "4",["۵"] = "5",["۶"] = "6",["۷"] = "7",["۸"] = "8",["۹"] = "9"
            };
            return LettersDictionary.Aggregate(persianStr, (current, item) =>
                         current.Replace(item.Key, item.Value));
      }
}

More info about Dictionary.Aggregate: Microsoft

Usage:

string result = "۱۰۳۶۷۵۱".PersianToEnglish();


回答2:

Simply Use the code below :

private string toPersianNumber(string input)
{
  string[] persian = new string[10] { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };

   for (int j=0; j<persian.Length; j++)
      input = input.Replace(persian[j], j.ToString());

   return input;
 }


回答3:

USE Culture To convert the number from any language to any language

Functions:

public static string ConvertDigitChar(this string str, CultureInfo source, CultureInfo destination)
{
    for (int i = 0; i <= 9; i++)
    {
        str = str.Replace(source.NumberFormat.NativeDigits[i], destination.NumberFormat.NativeDigits[i]);
    }
    return str;
}

public static string ConvertDigitChar(this int digit, CultureInfo destination)
{
    string res = digit.ToString();
    for (int i = 0; i <= 9; i++)
    {
        res = res.Replace(i.ToString(), destination.NumberFormat.NativeDigits[i]);
    }
    return res;
}

How to use the functions:

var fa = CultureInfo.GetCultureInfoByIetfLanguageTag("fa");
var en = CultureInfo.GetCultureInfoByIetfLanguageTag("en");
string str = "۰0۱1۲2۳3۴4۵5۶6۷7۸8۹9";
string r1 = str.ConvertDigitChar(en, fa);
string r2 = str.ConvertDigitChar(fa, en);
int i = 123456789;
string r3 = i.ConvertDigitChar(fa);

Result:

r1: "۰۰۱۱۲۲۳۳۴۴۵۵۶۶۷۷۸۸۹۹"

r2: "00112233445566778899"

r3: "۰۱۲۳۴۵۶۷۸۹"



回答4:

You need to parse them first, using e.g. Int32.Parse() with the correct cultural specifier. Once you have it as a plain integer, it's simply a matter of calling ToString() on it, again with the correct cultural specifier.

An alternative solution is to walk the string character by character and just replace any character that is a Persian digit with the corresponding (west) arabic numeral. Other characters can then be preserved as-is, if required.

If the string really contains a number, you should go with the integer parsing method. If it is not just a number, but really a phone number, serial number etc, you might need to use the replacing algorithm instead.



回答5:

You can manually convert them like so

    char[][] numbers = new char[][]
    {
        "0123456789".ToCharArray(),"persian numbers 0-9 here".ToCharArray()
    };
    public void Convert(string problem)
    {
        for (int x = 0; x <= 9; x++)
        {
            problem.Replace(numbers[0][x], numbers[1][x]);
        }
    }

I don't know the persian numbers so you will have to add them into the char array.



回答6:

I wrote this extension method to convert Arabic and Persian digits in an string to its Latin representation

public static class Extensions
{
    public static string ConvertDigitsToLatin(this string s)
    {
        var sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            switch (s[i])
            {
                    //Persian digits
                case '\u06f0':
                    sb.Append('0');
                    break;
                case '\u06f1':
                    sb.Append('1');
                    break;
                case '\u06f2':
                    sb.Append('2');
                    break;
                case '\u06f3':
                    sb.Append('3');
                    break;
                case '\u06f4':
                    sb.Append('4');
                    break;
                case '\u06f5':
                    sb.Append('5');
                    break;
                case '\u06f6':
                    sb.Append('6');
                    break;
                case '\u06f7':
                    sb.Append('7');
                    break;
                case '\u06f8':
                    sb.Append('8');
                    break;
                case '\u06f9':
                    sb.Append('9');
                    break;

                    //Arabic digits    
                case '\u0660':
                    sb.Append('0');
                    break;
                case '\u0661':
                    sb.Append('1');
                    break;
                case '\u0662':
                    sb.Append('2');
                    break;
                case '\u0663':
                    sb.Append('3');
                    break;
                case '\u0664':
                    sb.Append('4');
                    break;
                case '\u0665':
                    sb.Append('5');
                    break;
                case '\u0666':
                    sb.Append('6');
                    break;
                case '\u0667':
                    sb.Append('7');
                    break;
                case '\u0668':
                    sb.Append('8');
                    break;
                case '\u0669':
                    sb.Append('9');
                    break;
                default:
                    sb.Append(s[i]);
                    break;
            }
        }
        return sb.ToString();
    }
}


回答7:

The Saeed's Solution is Ok,But For Double Variables you Must Also replace "٫" Character To "." , So You Can Use :

private string ToEnglishNumber(string strNum)
{
string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "٫" };
string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","." };
string chash = strNum;
for (int i = 0; i < 11; i++)
    chash = chash.Replace(pn[i], en[i]);
return chash;
}


回答8:

Here my code convert Persian digits in variable to English , By extension method(Can use with dot after your expression)

private static readonly string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
    private static readonly string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    public static string ToEnglishNumber(this string strNum)
    {
        string chash = strNum;
        for (int i = 0; i < 10; i++)
            chash = chash.Replace(pn[i], en[i]);
        return chash;
    }
    public static string ToEnglishNumber(this int intNum)
    {
        string chash = intNum.ToString();
        for (int i = 0; i < 10; i++)
            chash = chash.Replace(pn[i], en[i]);
        return chash;
    }

and when you want to use this code have to write : txt1.Value.ToEnglishNumber();



回答9:

public static string ChangeNumberToEnglishNumber(string value)
    {
        string result=string.Empty;
        foreach (char ch in value)
        {

            try
            {
                double convertedChar = char.GetNumericValue(ch);
                if (convertedChar >= 0 && convertedChar <= 9)
                {
                    result += convertedChar.ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    result += ch;
                }


            }
            catch (Exception e)
            {
                result += ch;
            }

        }

        return result;
    }


回答10:

Use this extension ,also for arabic keyboard for example : "۵", "٥" or "۴", "٤"

static char[][] persianChars = new char[][]
    {
        "0123456789".ToCharArray(),"۰۱۲۳۴۵۶۷۸۹".ToCharArray()
    };
    static char[][] arabicChars = new char[][]
    {
        "0123456789".ToCharArray(),"٠١٢٣٤٥٦٧٨٩".ToCharArray()
    }; 
    public static string toPrevalentDigits(this string src)
    {
        if (string.IsNullOrEmpty(src)) return null;
        for (int x = 0; x <= 9; x++)
        {
            src = src.Replace(persianChars[1][x], persianChars[0][x]);
        }
        for (int x = 0; x <= 9; x++)
        {
            src = src.Replace(arabicChars[1][x], arabicChars[0][x]);
        }
        return src;
    } 


回答11:

You can use the Windows.Globalization.NumberFormatting.DecimalFormatter class to parse the string. This will parse strings in any of the supported numeral systems (as long as it is internally coherent).



回答12:

    public static string ToEnglishNumber(string input)
    {

        var englishnumbers = new Dictionary<string, string>()
        {
            {"۰","0" }, {"۱","1" }, {"۲","2" }, {"۳","3" },{"۴","4" }, {"۵","5" },{"۶","6" }, {"۷","7" },{"۸","8" }, {"۹","9" },
            {"٠","0" }, {"١","1" }, {"٢","2" }, {"٣","3" },{"٤","4" }, {"٥","5" },{"٦","6" }, {"٧","7" },{"٨","8" }, {"٩","9" },

        };

        foreach (var numbers in englishnumbers)
            input = input.Replace(numbers.Key, numbers.Value);

        return input;
    }


回答13:

Useful and concise:

public static class Utility
    {
        // '۰' = 1632
        // '0' = 48
        // ------------
        //  1632  => '۰'
        //- 1584
        //--------
        //   48   => '0'
        public static string GetEnglish(this string input)
        {
            char[] persianDigitsAscii = input.ToCharArray(); //{ 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641 };
            string output = "";
            for (int k = 0; k < persianDigitsAscii.Length; k++)
            {
                persianDigitsAscii[k] = (char) (persianDigitsAscii[k] - 1584);
                output += persianDigitsAscii[k];
            }

             return output;
        }
    }


回答14:

use this static class to change normalize number easily:

public static class Numbers
{
    public static string ChangeToEnglishNumber(this string text)
    {
        var englishNumbers = string.Empty;
        for (var i = 0; i < text.Length; i++)
        {
            if(char.IsNumber(text[i])) englishNumbers += char.GetNumericValue(text, i);
            else englishNumbers += text[i];
        }

        return englishNumbers;
    }
}

Sample:

string test = "۱۰۳۶۷۵۱".ChangeToEnglishNumber(); // => 1036751