如何使用文化将变量中的波斯数字转换为英文数字?

我想更改保存在变量中的波斯数字,如下所示:

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

 string Value="1036751"; 

我怎样才能使用像文化信息这样简单的方法来做到这一点?

我的示例代码是:

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

<=所以它不能解析为int
这是在我的函数中,它返回列表项中包含波斯数字的列表

 protected List ScrappingFunction(string SiteAddress, List NodesList) { string Price = "null"; List Targets = new List(); 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; } 

只需使用以下代码:

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

您需要首先解析它们,例如使用具有正确文化说明符的Int32.Parse()。 一旦你将它作为一个普通的整数,它只需要在它上面调用ToString(),再次使用正确的文化说明符。

另一种解决方案是逐字符串地处理字符串,并用相应的(西)阿拉伯数字替换任何波斯数字字符。 如果需要,可以按原样保留其他字符。

如果字符串确实包含数字 ,则应使用整数解析方法。 如果它不仅仅是一个数字,而是真正的电话号码,序列号等,您可能需要使用替换算法。

使用文化将数字从任何语言转换为任何语言

function:

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

如何使用function:

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

结果:

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

r2: "00112233445566778899"

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

您可以像这样手动转换它们

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

我不知道波斯数字,所以你必须将它们添加到char数组中。

我写了这个扩展方法,将字符串中的阿拉伯语和波斯语数字转换为拉丁语表示

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

这里我的代码将变量中的波斯数字转换为英文,通过扩展方法(可以在表达式后使用点)

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

当你想使用这段代码时必须写:txt1.Value.ToEnglishNumber();

Saeed的解决方案是好的,但对于双变量,您还必须将“。”字符替换为“。”。 ,所以你可以使用:

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

您可以使用Windows.Globalization.NumberFormatting.DecimalFormatter类来解析字符串。 这将解析任何支持的数字系统中的字符串(只要它在内部是连贯的)。

  public static string ToEnglishNumber(string input) { var englishnumbers = new Dictionary() { {"۰","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; } 
 string test = "۱۰۳۶۷۵۱"; string EnglishNumbers = ""; for (int i = 0; i < test.Length; i++) { EnglishNumbers += char.GetNumericValue(test, i); } int convertednumber = Convert.ToInt32(EnglishNumbers);