我正在尝试将数字转换为vb.net中的单词。但在某些地方我得到了Type Expected错误。以下是我的整个代码。我也将代码分别放在这个错误的地方。
Public Shared Function ConvertNumbertoWords(number As Integer) As String
If number = 0 Then
Return "ZERO"
End If
If number < 0 Then
Return Convert.ToString("minus ") & ConvertNumbertoWords(Math.Abs(number))
End If
Dim words As String = ""
If (number / 1000000) > 0 Then
words += ConvertNumbertoWords(number / 1000000) & Convert.ToString(" MILLION ")
number = number Mod 1000000
End If
If (number / 1000) > 0 Then
words += ConvertNumbertoWords(number / 1000) & Convert.ToString(" THOUSAND ")
number = number Mod 1000
End If
If (number / 100) > 0 Then
words += ConvertNumbertoWords(number / 100) & Convert.ToString(" HUNDRED ")
number = number Mod 100
End If
If number > 0 Then
If words <> "" Then
words += "AND "
End If
Dim unitsMap = New () {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN",
"TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN",
"EIGHTEEN", "NINETEEN"}
Dim tensMap = New () {"ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY"}
If number < 20 Then
words += unitsMap(number)
Else
words += tensMap(number / 10)
If (number Mod 10) > 0 Then
words += " " + unitsMap(number Mod 10)
End If
End If
End If
Return words
End Function
在下面的行中,我收到预期类型错误
Dim unitsMap = New () {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN",
"TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN",
"EIGHTEEN", "NINETEEN"}
Dim tensMap = New () {"ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY"}
答案 0 :(得分:1)
您收到此错误是因为您没有指定要声明的数组类型。当我尝试使用此错误时没有错误
Dim unitsMap = New string() {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN","EIGHTEEN", "NINETEEN"}
希望这会有所帮助。
答案 1 :(得分:0)
尽管number是一个整数,但是数字/ 1000000返回一个值为.003256的double(当数字为3256时),这是&gt; 0
对于整数除法,请使用\
If (number \ 1000000) > 0 Then