我有一个文本框,单击计算器上的相应按钮可显示数字和运算符,然后单击=按钮即可得到结果。
问题是,我试图让它在数字超过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
答案 0 :(得分:0)
问题是,在按下2019-01-01 13:00:01 2.0
2019-01-01 13:00:02 3.0
2019-01-01 13:00:03 3.0
2019-01-01 13:00:04 4.0
2019-01-01 13:00:05 1.0
2019-01-01 13:00:06 2.0
操作符之后,您的文本框可能包含两个数字。如果要格式化它们,则需要将字符串分成第一个数字,运算符和第二个数字,然后分别格式化两个数字。 timestamps = [
'2019-01-01 13:00:00.060000', #0
'2019-01-01 13:00:00.140000', #0
'2019-01-01 13:00:00.731000', #0
'2019-01-01 13:00:01.135000', #1
'2019-01-01 13:00:01.344000', #1
'2019-01-01 13:00:02.174000', #2
'2019-01-01 13:00:02.213000', #2
'2019-01-01 13:00:02.363000', #2
'2019-01-01 13:00:02.951000', #2
'2019-01-01 13:00:03.393000', #3
'2019-01-01 13:00:03.454000', #3
'2019-01-01 13:00:04.444000', #4
'2019-01-01 13:00:05.123000', #5
'2019-01-01 13:00:05.456000', #5
]
df = pd.DataFrame([1, 2, 1, 2, 3, 2, 3, 2, 1, 4, 4, 4, 1 ,4]
,columns=['A'], index=pd.to_datetime(timestamps)
和+
可以包含类似val1
的内容,并且不能用val2
进行转换。
我还将逻辑提取到可重用的方法中,而不是一遍又一遍地针对不同的按钮进行重复。
该文本框应仅用于显示,而不能用作输入的存储。
"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
。请注意,我们有3种输入法。他们每个人最后都叫Calculate
。 Display
将进行所有格式化并将结果放入文本框中。
点击事件处理程序:
Display
Display
方法非常棘手,因为我们可能必须格式化不完整的数字。例如。以小数点开头或结尾的数字或带有尾随零的数字。我们必须确保在格式化过程中保留这些内容。我结束了
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
方法中那样将输入拆分为多个部分。