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

23
我在一个使用C#的项目中需要使用阿拉伯数字,但是必须将其存储为整数类型在数据库中。我需要一个在C#中将阿拉伯数字转换为整数的解决方案。请问有什么解决方案或帮助可以提供吗?谢谢!
从评论中可以得知,您有如١،٢،٣,٤等阿拉伯数字,并需将其转换为1,2,3或将٢٣٤转换为234。

3
所有数字都是阿拉伯数字吗?你可能正在尝试转换阿拉伯符号。 - Sebastjan
你能展示一下你已经尝试过的代码吗? - James Khoury
并请给出一个示例输入。 - H H
8个回答

31

更新: 你可以使用 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;
}

4
我很喜欢这个,因为你可以翻译任何“字符串”,即使它不是像日期等“全是数字”的字符串。 - Ehsan88

10

遗憾的是目前还不能通过传入适当的IFormatProvider来解析完整的字符串表示(也许在即将发布的版本中会有这个功能)。但是,char类型有一个GetNumericValue方法,可以将任何数字Unicode字符转换为double类型。例如:

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

9
阿拉伯数字如١،٢،٣、٤在Unicode中被编码为范围在1632到1641的字符。从每个阿拉伯数字字符的Unicode值中减去阿拉伯零的Unicode(1632),以获得它们的数字值。将每个数字值与其位值相乘,并求和以获得整数。
或者使用Regex.Replace将带有阿拉伯数字的字符串转换为带有十进制数字的字符串,然后使用Int.Parse将结果转换为整数。

我猜int.Parse可以处理阿拉伯代码点而不需要特殊处理。然而,我手头没有C#编译器来检查它。我相当确定char.IsNumeric测试对这些字符返回true。你有检查过你建议的技术是否在这里是必要的吗? - phoog
是的,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);说缓冲区太小或太大。 - Shaahin
各位好,感谢帮助: 我找到了另一种解决方案,可以使用 Switch 而不是转换。再次感谢。 - Shaahin
1
如果您打算使用建议的任何方法,请记住Unicode中阿拉伯数字有两个范围(波斯语实际上只有一个)! https://dev59.com/zXI-5IYBdhLWcg3wxrqQ - Ali

9
将阿拉伯数字转换为整数的简单方法
    string EnglishNumbers="";
    for (int i = 0; i < arabicnumbers.Length; i++)
    {
        EnglishNumbers += char.GetNumericValue(arabicnumbers, i);
    }
    int convertednumber=Convert.ToInt32(EnglishNumbers);

3
这是我的解决方案:
public static string arabicNumToEnglish(string input)
{
    String[] map={"٠","١","٢","٣","٤","٥","٦","٧","٨","٩"};
    
    for (int i = 0; i <= 9; i++)
    {
        input=input.Replace(map[i],i.ToString());
    }
    
    return input;
}

0

要获取数字的值,需要从中减去零字符,例如在普通数字中,'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按顺序编码,就可以适用于任何数字符号。


这仍然不太符合国际化标准;尼泊尔或中国数字怎么办? - Luis Filipe

0

试试这个扩展:

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

0

我知道这个问题有点老了,但是在我的一个项目中我遇到了类似的情况,看到了这个问题并决定分享我的解决方案,它对我来说完美地解决了问题,希望它也能为其他人提供帮助。

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

现在你只需调用该函数便可返回西阿拉伯数字。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接