将阿拉伯数字转换为英语

时间:2012-03-20 11:17:03

标签: vb.net numeric-conversion

我正在寻找一种方法将阿拉伯数字字符串“0123456789”转换为英语 数字字符串“0123456789”

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       dim Anum as string ="٠١٢٣٤٥٦٧٨٩"
       dim Enum as string =get_egnlishNum(Anum)
End Sub

private function get_egnlishNum(byval _Anum as string) as string

''   converting code

end function

4 个答案:

答案 0 :(得分:3)

您正在寻找GetNumericValue类型的char方法,它将任何数字Unicode字符转换为double。例如:

double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2

对于你的例子:

static string ArabicToWestern(string input)
{
    StringBuilder western = new StringBuilder();
    foreach(char num in input)
    {
        western.Append(char.GetNumericValue(num));
    }
    return western.ToString();
}

根据您的需要进行修改。

VB.NET:

Private Shared Function ArabicToWestern(ByVal input As String) As String
    Dim western As StringBuilder = New StringBuilder
    For Each num As Char In input
        western.Append(Char.GetNumericValue(num))
    Next
    Return western.ToString
End Function

答案 1 :(得分:2)

这是其中一个解决方案。

Function Convert(ByVal input As String) As String
    Dim source = "٠١٢٣٤٥٦٧٨٩"
    Dim target = "0123456789"
    Dim sb As New StringBuilder()
    For Each el in Input
        sb.Append(target(source.IndexOf(el)))
    Next
    Return sb.ToString
End Function

修改

我试图找出更多“原生”方式。我发现的是NativeDigits

NumberFormatInfo属性

这是我的测试代码,但没有成功。但这可能是一个很好的起点。

        Dim source = "١٢٣٤٥٦٧٨٩"
        Dim result As Integer
        Dim numInfo As new NumberFormatInfo()
        numInfo.NativeDigits = New String() { "٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩" }
        Int32.TryParse(source, NumberStyles.Any, numInfo, result)

答案 2 :(得分:1)

您只需使用西方版本替换阿拉伯字符:

Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
Dim s = "‏٠٦٦٢٧٣٩٦عدد النقاط هي ٩٩٣٩٣٥"

For i = 0 To arabicDigits.Length - 1
    s = s.Replace(arabicDigits(i), i.ToString)
Next

现在包含“06627396عددالنقاطهي993935”

答案 3 :(得分:0)

Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
      dim i as integer=0
For i = 0 To anum.Length - 1
select case arabicDigits(i)
case "٠" 
Replace(arabicDigits(i),"0")
case "١"
Replace(arabicDigits(i),"1")
case "٢"
Replace(arabicDigits(i),"2")
case "٣"
Replace(arabicDigits(i),"3")
case "٤"
Replace(arabicDigits(i),"4")
case "٥"
Replace(arabicDigits(i),"5")
case "٦"
Replace(arabicDigits(i),"6")
case "٧"
Replace(arabicDigits(i),"7")
case "٨"
Replace(arabicDigits(i),"8")
case "٩"
Replace(arabicDigits(i),"9")
i=i+1

endselect
End Sub