将数字转换为文本(2个现有单词,相同的数字)

时间:2015-04-05 14:27:38

标签: vb.net vb.net-2010

我正在尝试将数值结果转换为单词。

我找到的样本适用于英语单词,但随着我将其更改为我的语言,出现了一堆肤浅的问题。

我需要特别帮助的是为100构建与10相同的代码。我这样做的原因是因为在我的语言中,我们没有One站在每个Hundred / Thousand

前面

例如:假设Ones中的第一个数字称为Taff

实际上,该程序会使Taff Hundred成为Taffss Hundred

之类的内容

所以我创建了包含正确调用的Hundreds()

  • Ones()包含1 - 9
  • 中的单词
  • 青少年()包含来自的单词 11 - 19
  • Tens()包含10 - 90(10,20,30等)
  • 中的单词
  • 数百()包含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)

作为我的菜鸟,我认为通过将代码粘贴10s并将值更改为100s会使事情有效,但它仅适用于100 200 {{1}等等,而不是中间的数字。

300

更新 我做了一些改动,这次它部分有效...... 它显示100s,200s,300s等。 但它只显示从100到109,当它“增量”如果它超过109,100s不显示,它又回到显示10s,11s,12s等。

              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

1 个答案:

答案 0 :(得分:1)

做了一些改变......这更接近你之后的事吗?

    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