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

后端 未结 15 2132
终归单人心
终归单人心 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:48
        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;
        }
    
    0 讨论(0)
  • 2021-02-02 07:50

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

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

    0 讨论(0)
提交回复
热议问题