将数字转换为文本(2个存在的单词,相同的数字)
Converting numbers to text (2 existent words, same number)
我正在尝试将数值结果转换为文字。
我找到的示例对于英文单词效果很好,但是当我将它们更改为我的语言时,出现了一堆表面问题。
我特别需要帮助的是为 100
构建与 10
相同的代码。我这样做的原因是因为在我的语言中我们没有 One
站在每个 Hundred
/Thousand
前面
例如:假设 Ones
中的第一个数字称为 Taff
事实上,程序会在它应该制作的地方制作 Taff Hundred
制作类似 Taffss Hundred
的内容
所以我创建了包含正确调用的 Hundreds()
。
- Ones() 包含 1 - 9 中的单词
- Teens() 包含来自
11 - 19
- Tens() 包含从 10 到 90 的单词(10、20、30 等)
- Hundreds() 包含 100-900 的单词(100,200,300 等)
这是我的代码:
intAmount = eAmount
Dim nTousands As Integer = intAmount \ 1000 : intAmount = intAmount Mod 1000
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1
If nTen > 0 Then
If nTen = 1 And nOne > 0 Then
wAmount = wAmount & Teens(nOne) & " "
Else
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
End If
ElseIf nOne > 0 Then
wAmount = wAmount & Ones(nOne) & " "
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
作为菜鸟,我认为通过复制粘贴代码 10 秒,并将值更改为 100 秒可以使事情正常进行,但它仅适用于 100
200
300
等,不适用于中间的数字。
ElseIf nHundred > 0 Then
If nHundred = 1 And nOne > 0 Then
'Don't know how to properly add things here.
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
更新
我做了一些改变,这次它部分起作用了……
它显示 100s、200s、300s 等。
但它只显示从 100 到 109 时 "increments" 如果超过 109,则不会显示 100,然后返回显示 10、11、12 等
ElseIf nHundred > 0 Then
If nHundred = 1 And nTen > 0 And nOne > 0 Then
wAmount = wAmount & Teens(nTen) & " "
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
进行了一些更改...这是否更接近您的要求?
Private list_ones() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private list_teens() As String = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
Private list_tens() As String = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Private list_hundreds() As String = {"ones", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private Function get_string_from_numeric(ByVal curr_number As Integer) As String
Dim to_return As String = ""
Try
Select Case curr_number
Case Is < 10
to_return = list_ones(curr_number)
Case 10 To 19
to_return = list_teens(curr_number - 10)
Case 20 To 99
to_return = get_return_value(curr_number, 10, list_tens)
Case 100 To 999
to_return = get_return_value(curr_number, 100, list_hundreds, "hundred")
Case 1000 To 9999
to_return = get_return_value(curr_number, 1000, list_hundreds, "thousand")
Case Is > 9999
to_return = "out of range"
End Select
Catch
Finally
End Try
Return to_return
End Function
Private Function get_return_value(ByVal curr_number As Integer, ByVal curr_pace_holder As Integer, ByVal curr_array_list As String(), Optional ByVal str_term As String = "") As String
Dim to_return As String = ""
Dim curr_iter As Integer = Math.Floor(curr_number / curr_pace_holder)
Dim curr_remainder As Integer = curr_number - curr_iter * curr_pace_holder
If (str_term <> "") Then str_term = " " & str_term
If (curr_remainder > 0) Then
to_return = curr_array_list(curr_iter - 1) & str_term & " " & get_string_from_numeric(curr_remainder)
Else
to_return = curr_array_list(curr_iter - 1) & str_term
End If
Return to_return
End Function
Private Sub cmd_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_submit.Click
Try
If (IsNumeric(txt_input.Text)) Then
txt_output.Text = get_string_from_numeric(CInt(txt_input.Text))
Else
MsgBox("Invalid input.")
End If
Catch
Finally
End Try
End Sub
我正在尝试将数值结果转换为文字。
我找到的示例对于英文单词效果很好,但是当我将它们更改为我的语言时,出现了一堆表面问题。
我特别需要帮助的是为 100
构建与 10
相同的代码。我这样做的原因是因为在我的语言中我们没有 One
站在每个 Hundred
/Thousand
例如:假设 Ones
中的第一个数字称为 Taff
事实上,程序会在它应该制作的地方制作 Taff Hundred
制作类似 Taffss Hundred
所以我创建了包含正确调用的 Hundreds()
。
- Ones() 包含 1 - 9 中的单词
- Teens() 包含来自 11 - 19
- Tens() 包含从 10 到 90 的单词(10、20、30 等)
- Hundreds() 包含 100-900 的单词(100,200,300 等)
这是我的代码:
intAmount = eAmount
Dim nTousands As Integer = intAmount \ 1000 : intAmount = intAmount Mod 1000
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1
If nTen > 0 Then
If nTen = 1 And nOne > 0 Then
wAmount = wAmount & Teens(nOne) & " "
Else
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
End If
ElseIf nOne > 0 Then
wAmount = wAmount & Ones(nOne) & " "
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
作为菜鸟,我认为通过复制粘贴代码 10 秒,并将值更改为 100 秒可以使事情正常进行,但它仅适用于 100
200
300
等,不适用于中间的数字。
ElseIf nHundred > 0 Then
If nHundred = 1 And nOne > 0 Then
'Don't know how to properly add things here.
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
更新 我做了一些改变,这次它部分起作用了…… 它显示 100s、200s、300s 等。 但它只显示从 100 到 109 时 "increments" 如果超过 109,则不会显示 100,然后返回显示 10、11、12 等
ElseIf nHundred > 0 Then
If nHundred = 1 And nTen > 0 And nOne > 0 Then
wAmount = wAmount & Teens(nTen) & " "
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
进行了一些更改...这是否更接近您的要求?
Private list_ones() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private list_teens() As String = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
Private list_tens() As String = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Private list_hundreds() As String = {"ones", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private Function get_string_from_numeric(ByVal curr_number As Integer) As String
Dim to_return As String = ""
Try
Select Case curr_number
Case Is < 10
to_return = list_ones(curr_number)
Case 10 To 19
to_return = list_teens(curr_number - 10)
Case 20 To 99
to_return = get_return_value(curr_number, 10, list_tens)
Case 100 To 999
to_return = get_return_value(curr_number, 100, list_hundreds, "hundred")
Case 1000 To 9999
to_return = get_return_value(curr_number, 1000, list_hundreds, "thousand")
Case Is > 9999
to_return = "out of range"
End Select
Catch
Finally
End Try
Return to_return
End Function
Private Function get_return_value(ByVal curr_number As Integer, ByVal curr_pace_holder As Integer, ByVal curr_array_list As String(), Optional ByVal str_term As String = "") As String
Dim to_return As String = ""
Dim curr_iter As Integer = Math.Floor(curr_number / curr_pace_holder)
Dim curr_remainder As Integer = curr_number - curr_iter * curr_pace_holder
If (str_term <> "") Then str_term = " " & str_term
If (curr_remainder > 0) Then
to_return = curr_array_list(curr_iter - 1) & str_term & " " & get_string_from_numeric(curr_remainder)
Else
to_return = curr_array_list(curr_iter - 1) & str_term
End If
Return to_return
End Function
Private Sub cmd_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_submit.Click
Try
If (IsNumeric(txt_input.Text)) Then
txt_output.Text = get_string_from_numeric(CInt(txt_input.Text))
Else
MsgBox("Invalid input.")
End If
Catch
Finally
End Try
End Sub