How to Convert Persian Digits in variable to Engli

2020-05-19 19:55发布

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

14条回答
老娘就宠你
2楼-- · 2020-05-19 20:34

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;
}
查看更多
劫难
3楼-- · 2020-05-19 20:35

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;
        }
    }
查看更多
疯言疯语
4楼-- · 2020-05-19 20:36

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

查看更多
三岁会撩人
5楼-- · 2020-05-19 20:38

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
查看更多
【Aperson】
6楼-- · 2020-05-19 20:39

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;
 }
查看更多
等我变得足够好
7楼-- · 2020-05-19 20:40

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();
查看更多
登录 后发表回答