C# 如何将阿拉伯数字转换为整数?

C# 如何将阿拉伯数字转换为整数?,c#,C#,我在C#中从事一个项目,该项目需要使用阿拉伯数字,但它必须以整数形式存储在数据库中,我需要一个解决方案将阿拉伯数字转换为C#中的整数。 有什么解决办法或帮助吗? 提前谢谢 从评论中: 我有阿拉伯数字,像١،،٣،٤。。。必须转换为1,2,3,或转换为234 unicode中的阿拉伯数字(如١،٢٣،٤)被编码为1632到1641之间的字符。从每个阿拉伯数字字符的unicode值中减去(1632)以获得其数字值。将每个数字值与其位置值相乘,并将结果相加得到整数 或者使用Regex.Replace

我在C#中从事一个项目,该项目需要使用阿拉伯数字,但它必须以整数形式存储在数据库中,我需要一个解决方案将阿拉伯数字转换为C#中的整数。 有什么解决办法或帮助吗? 提前谢谢


从评论中:

我有阿拉伯数字,像١،،٣،٤。。。必须转换为1,2,3,或转换为234


unicode中的阿拉伯数字(如١،٢٣،٤)被编码为1632到1641之间的字符。从每个阿拉伯数字字符的unicode值中减去(1632)以获得其数字值。将每个数字值与其位置值相乘,并将结果相加得到整数


或者使用
Regex.Replace
将带阿拉伯数字的字符串转换为带十进制数字的字符串,然后使用
Int.Parse
将结果转换为整数。

要获取数字的值,从中减去零字符,例如在普通数字中,
'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是连续编码的。

将阿拉伯数字转换为整数的简单方法

    string EnglishNumbers="";
    for (int i = 0; i < arabicnumbers.Length; i++)
    {
        EnglishNumbers += char.GetNumericValue(arabicnumbers, i);
    }
    int convertednumber=Convert.ToInt32(EnglishNumbers);
string englishNumber=“”;
for(int i=0;i
不幸的是,通过传递适当的
IFormatProvider(可能在即将发布的版本中),还无法解析完整的字符串表示形式。但是,
char
类型有一个
GetNumericValue
方法,可以将任何数字Unicode字符转换为双精度字符。例如:

double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2
您可以使用它一次转换一个数字。

使用此方法

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;
}
私有字符串到英语数字(字符串输入)
{
字符串英文数字=”;
for(int i=0;i
我知道这个问题有点老了,但是我在一个项目中遇到了类似的情况,我忽略了这个问题,决定与大家分享我的解决方案,这个方案对我来说非常有效,希望它也能为其他人服务

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", "۹");
        }
    }

不是所有的数字都是阿拉伯语吗?您可能正在尝试转换阿拉伯语SYBOL。您能显示您已经尝试过的任何代码吗?请给出一个输入示例。我猜int.Parse可以处理阿拉伯语代码点,无需特殊处理。然而,我手头没有C#编译器来检查它。我相当确信char.IsNumeric测试对于这些字符返回true。你是否检查过你建议的技术之一在这里是必要的?是的,Tarje我认为你的解决方案是正确的,我可以提取阿拉伯数字的值,例如4(٤)的值是1636,但现在我无法将该值转换为in,这是我的代码:
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);表示缓冲区太小或太大。亲爱的所有人,感谢帮助:我找到了其他解决方案,我可以使用交换机代替对话。再次感谢如果您要使用任何建议的方法,请记住Unicode中的阿拉伯数字有两个范围(实际上一个是波斯语)!非常喜欢这一点,因为你可以翻译任何
字符串
,即使它不是日期等“所有数字”。这看起来仍然不是很国际化;尼泊尔或中国的数字呢?