在计算器的单个文本框中显示大数字的逗号

Display commas for large numbers in a single textbox of a calculator

我有一个文本框,在单击计算器上的相应按钮时显示数字和运算符,然后在单击 = 按钮时得到结果。

问题是,我试图让它在数字超过 999.99 时显示逗号并计算它们。它适用于一个带逗号但不带逗号的数字,只有那些没有逗号的数字(只要它们是最后输入的)。

例如:

1,111 + 33333 (works in calculation)
1,111 + 22,222 (doesn't work)
33333 + 1,111 (doesn't work either)

这是我的一些代码:

Private Sub ButtonClickMethod(sender As Object, e As EventArgs) Handles Button0.Click, Button1.Click, Button2.Click, Button3.Click, PlusButton.Click, EqualButton.Click
Dim button As Button = CType(sender, Button)

    'numbers
    If button.Name = "Button0" Then
        TextBox.Text = TextBox.Text + "0"
    End If
    If button.Name = "Button1" Then
        Dim val1 As String
        val1 = TextBox.Text + "1"
        TextBox.Text = val1
        TextBox.Text = Convert.ToDecimal(val1).ToString("#,##")
    End If
    If button.Name = "Button2" Then
        Dim val2 As String
        val2 = TextBox.Text + "2"
        TextBox.Text = val2
        TextBox.Text = Convert.ToDecimal(val2).ToString("#,##")
    End If
    If button.Name = "Button3" Then
        TextBox.Text = TextBox.Text + "3"
    End If
    'operations
    If button.Name = "PlusButton" Then
        TextBox.Text = TextBox.Text + "+"
    End If
    'equal/result
    If button.Name = "EqualButton" Then
        Dim equation As String = TextBox.Text.Replace(",", String.Empty)
        Dim results = New DataTable().Compute(equation, Nothing)
        TextBox.Text = results
    End If
End Sub

问题是,在按下 + 运算符后,您的文本框可能包含两个数字。如果要格式化它们,则需要将字符串分成第一个数字、运算符和第二个数字,并分别格式化这两个数字。 val1val2 可以包含类似 "13.2+5" 的内容,并且不能用 ToDecimal.

转换

我还会将逻辑提取到可重用的方法中,而不是为不同的按钮一遍又一遍地重复它。

文本框只能用于显示,不能作为输入的存储。

Private _input As String

Private Sub TypeDigit(ByVal key As Char)
    _input += key
    Display()
End Sub

Private Sub TypeOperator(ByVal key As Char)
    _input += " " & key & " "
    Display()
End Sub

Private Sub Execute(ByVal key As Char)
    Select Case key
        Case "="c
            _input = Calculate(_input)
        Case "C"c
            _input = ""
        Case Else
    End Select

    Display()
End Sub

我们仍然要声明CalculateDisplay这两个方法。请注意,我们有 3 种输入法。他们每个人都在最后调用 DisplayDisplay 将进行所有格式设置并将结果放入文本框中。

点击事件处理程序:

Private Sub buttonClear_Click(ByVal sender As Object, ByVal e As EventArgs)
    Execute("C"c)
End Sub

Private Sub buttonDecimalPoint_Click(ByVal sender As Object, ByVal e As EventArgs)
    TypeDigit("."c)
End Sub

Private Sub button0_Click(ByVal sender As Object, ByVal e As EventArgs)
    TypeDigit("0"c)
End Sub

...

Private Sub button9_Click(ByVal sender As Object, ByVal e As EventArgs)
    TypeDigit("9"c)
End Sub

Private Sub buttonEquals_Click(ByVal sender As Object, ByVal e As EventArgs)
    Execute("="c)
End Sub

Private Sub buttonPlus_Click(ByVal sender As Object, ByVal e As EventArgs)
    TypeOperator("+"c)
End Sub

' other operators here...

Display方法比较棘手,因为我们可能需要格式化不完整的数字。例如。以小数点开头或结尾的数字或尾随零的数字。我们必须确保在格式化期间保留这些内容。我最终得到了

Private Sub Display()
    Dim parts As String() = _input.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    Dim display As String = ""
    Dim n As Decimal

    For Each part As String In parts
        If part = "." Then
            display += "0."
        ElseIf part.EndsWith(".") AndAlso Decimal.TryParse(part.Substring(0, part.Length - 1), n) Then
            display += n.ToString("#,##0") & "."
        ElseIf Decimal.TryParse(part, n) Then
            Dim decimalPoint As Integer = part.IndexOf("."c)

            If decimalPoint >= 0 Then
                Dim numDecimals As Integer = part.Length - decimalPoint - 1
                display += n.ToString("#,##0." & New String("0"c, numDecimals))
            Else
                display += n.ToString("#,##0")
            End If
        Else
            display += " " & part & " "
        End If
    Next

    textBoxDisplay.Text = display
End Sub

我没有实现 Calculate 方法。我把它留给你。您必须像 Display 方法一样,将输入分成几部分。