我在一个使用C#的项目中需要使用阿拉伯数字,但是必须将其存储为整数类型在数据库中。我需要一个在C#中将阿拉伯数字转换为整数的解决方案。请问有什么解决方案或帮助可以提供吗?谢谢!
从评论中可以得知,您有如١،٢،٣,٤等阿拉伯数字,并需将其转换为1,2,3或将٢٣٤转换为234。
从评论中可以得知,您有如١،٢،٣,٤等阿拉伯数字,并需将其转换为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类型。例如:
double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2
你可以使用它来逐个转换数字。Regex.Replace
将带有阿拉伯数字的字符串转换为带有十进制数字的字符串,然后使用Int.Parse
将结果转换为整数。if (char.IsDigit(c))
{
bytes[1] = Convert.ToByte(char.GetNumericValue(c));
utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
convertedChars.Append(convertedChar[0]);
}
在代码中:utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);说缓冲区太小或太大。 - Shaahin 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按顺序编码,就可以适用于任何数字符号。
试试这个扩展:
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", "۹");
}
}
我知道这个问题有点老了,但是在我的一个项目中我遇到了类似的情况,看到了这个问题并决定分享我的解决方案,它对我来说完美地解决了问题,希望它也能为其他人提供帮助。
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();
}