如何将阿拉伯数字转换为整型
本文关键字:整型 转换 阿拉伯数字 | 更新日期: 2023-09-27 17:54:28
我在c#中工作,需要使用阿拉伯数字,但必须在数据库中存储为整数,我需要一个解决方案将阿拉伯数字转换为c#中的int。有什么解决方案或帮助吗?提前感谢
从评论:
我有阿拉伯数字像١،٢،٣،٤…必须转换为1、2、3或٢٣٤转换为234
已更新:您可以使用StringBuilder进行内存优化。
private static string ToEnglishNumbers(string input)
{
StringBuilder sbEnglishNumbers = new StringBuilder(string.Empty);
for (int i = 0; i < input.Length; i++)
{
if (char.IsDigit(input[i]))
{
sbEnglishNumbers.Append(char.GetNumericValue(input, i));
}
else
{
sbEnglishNumbers.Append(input[i].ToString());
}
}
return sbEnglishNumbers.ToString();
}
原始答案:使用此方法
private string toEnglishNumber(string input)
{
string EnglishNumbers = "";
for (int i = 0; i < input.Length; i++)
{
if (Char.IsDigit(input[i]))
{
EnglishNumbers += char.GetNumericValue(input, i);
}
else
{
EnglishNumbers += input[i].ToString();
}
}
return EnglishNumbers;
}
不幸的是,还不可能通过传入适当的IFormatProvider
来解析完整的字符串表示(可能在即将到来的版本中)。然而,char
类型有一个GetNumericValue
方法,它可以将任何Unicode数字字符转换为双精度类型。例如:
double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2
您可以使用它一次转换一个数字
阿拉伯数字如١،٢،٣،٤在unicode中被编码为1632到1641范围内的字符。从每个阿拉伯数字字符的unicode值中减去阿拉伯零(1632)的unicode,得到它们的数字值。将每个数字值与其位值相乘,并将结果相加得到整数。
或者使用Regex.Replace
将带有阿拉伯数字的字符串转换为带有十进制数字的字符串,然后使用Int.Parse
将结果转换为整数
将阿拉伯数字转换为整数的简单方法
string EnglishNumbers="";
for (int i = 0; i < arabicnumbers.Length; i++)
{
EnglishNumbers += char.GetNumericValue(arabicnumbers, i);
}
int convertednumber=Convert.ToInt32(EnglishNumbers);
这是我的解决方案:
public static string arabicNumToEnglish(string input)
{
String[] map={"٠","١","٢","٣","٤","٥","٦","٧","٨","٩"};
for (int i = 0; i <= 9; i++)
{
input=input.Replace(map[i],i.ToString());
}
return input;
}
要得到一个数字的值,从它减去零字符,例如在普通数字中,'1'-'0' = 1
, '2'-'0' = 2
。等。
对于多位数,可以这样写
result =0;
foreach(char digit in number)
{
result *= 10; //shift the digit, multiply by ten for each shift
result += (digit - '0)'; //add the int value of the current digit.
}
如果您的数字使用阿拉伯字符,只需将'0'替换为阿拉伯零。这适用于任何数字符号,只要该符号系统中的0-9是连续编码的。
我知道这个问题有点老了,但是我在我的一个项目中遇到了类似的情况,并且通过了这个问题,并决定分享我的解决方案,它对我来说非常有效,并希望它将为其他人提供同样的服务。
private string ConvertToWesternArbicNumerals(string input)
{
var result = new StringBuilder(input.Length);
foreach (char c in input.ToCharArray())
{
//Check if the characters is recognized as UNICODE numeric value if yes
if (char.IsNumber(c))
{
// using char.GetNumericValue() convert numeric Unicode to a double-precision
// floating point number (returns the numeric value of the passed char)
// apend to final string holder
result.Append(char.GetNumericValue(c));
}
else
{
// apend non numeric chars to recreate the orignal string with the converted numbers
result.Append(c);
}
}
return result.ToString();
}
现在您可以简单地调用该函数来返回西方阿拉伯数字
试试这个扩展:
public static class Extension
{
public static string ToEnglishNumbers(this string s)
{
return s.Replace("۰", "0").Replace("۱", "1").Replace("۲", "2").Replace("۳", "3").Replace("۴", "4")
.Replace("۵", "5").Replace("۶", "6").Replace("۷", "7").Replace("۸", "8").Replace("۹", "9");
}
public static int ToNumber(this string s)
{
if (int.TryParse(s.ToEnglishNumbers(), out var result))
{
return result;
}
return -1;
}
public static string ToArabicNumbers(this string s)
{
return s.Replace("0", "۰").Replace("1", "۱").Replace("2", "۲").Replace("3", "۳").Replace("4", "۴")
.Replace("5", "۵").Replace("6", "۶").Replace("7", "۷").Replace("8", "۸").Replace("9", "۹");
}
}