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

后端 未结 15 2130
终归单人心
终归单人心 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:27

    there is a simple way to do this

    public static string Fa2En(string str)
    {
      return str.Replace("۰", "0")
                .Replace("۱", "1")
                .Replace("۲", "2")
                .Replace("۳", "3")
                .Replace("۴", "4")
                .Replace("۵", "5")
                .Replace("۶", "6")
                .Replace("۷", "7")
                .Replace("۸", "8")
                .Replace("۹", "9");
    }
    
    0 讨论(0)
  • 2021-02-02 07:28

    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.

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

    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: "۰۱۲۳۴۵۶۷۸۹"

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

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

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