How to Convert Persian Digits in variable to English Digits Using Culture?

后端 未结 15 2133
终归单人心
终归单人心 2021-02-02 07:10

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

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

to

string Value=\"1036751\"         


        
相关标签:
15条回答
  • 2021-02-02 07:35

    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
    
    0 讨论(0)
  • 2021-02-02 07:37

    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.

    0 讨论(0)
  • 2021-02-02 07:40

    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;
            }
        }
    
    0 讨论(0)
  • 2021-02-02 07:43

    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();
    
    0 讨论(0)
  • 2021-02-02 07:46

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

    0 讨论(0)
  • 2021-02-02 07:48

    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;
    }
    
    0 讨论(0)
提交回复
热议问题