【发布时间】:2017-03-19 12:01:01
【问题描述】:
我正在添加两个十进制数字。当小数部分达到 0.60 时,应向上取整,例如 20.60 向上取整为 21.00。
我已经能够做到这一点,并且该应用程序大部分都在工作,但是当小数点前的数字超过百列时,比如说 999.40,并且它到达千位列,千位分隔符不会工作。例如,如果我想将两个数字相加,比如 600.20 和 600.30,我的答案不是 1,200.50,而是 1.00。
这是我目前的代码:
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
'ans for txtbox5.tetx
Try
If Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label41.Text).ToString("N2")), 2)) + Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label42.Text).ToString("N2")), 2)) > 99 Then
MessageBox.Show("invalid entry")
Else
Label41.Text = Val(TxtBox2.Text)
Label42.Text = Val(TxtBox34.Text)
'sum of numbers in two txtbox
'TxtBox5.Text = Val(TxtBox2.Text) + Val(TxtBox34.Text)
Label43.Text = (Val(Label41.Text) + Val(Label42.Text)).ToString("N2")
'strip last 2 decimals :
ln = Convert.ToInt32(Microsoft.VisualBasic.Right((Val(Label43.Text).ToString("N2")), 2))
Label44.Text = ln.ToString
'form decimal from 2 decimals
Label45.Text = "0." & Label44.Text
'subtract new decimal from 1st answer
Label46.Text = (Val(Label43.Text) - Val(Label45.Text)).ToString("N2")
'checks if striped decimal is btw 100 and 59
If (Val(Label44.Text)) < 100 And (Val(Label44.Text)) > 59 Then
runup = runup + 1
newans = (Val(Label44.Text) - Val(60))
Label45.Text = (Val(Label45.Text) - Val(0.6)).ToString("N2")
Try
'check if decimal is between 100 and 59
If (Val(newans)) < 100 And (Val(newans)) > 59 Then
runup = runup + 1
newans = (Val(newans) - Val(60))
Label45.Text = (Val(Label45.Text) - Val(0.6)).ToString("N2")
Label47.Text = (Val(runup) + Val(Label46.Text)) + Val(Label45.Text).ToString("N2")
runup = 0
'check if new decimal is between 60 and 0
ElseIf (Val(newans)) < 60 And (Val(newans)) >= 0 Then
Label47.Text = ((Val(runup) + Val(Label46.Text)) + Val(Label45.Text)).ToString("N2")
runup = 0
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'check if striped decimal is btw 60 and 0
ElseIf (Val(Label44.Text)) < 60 And (Val(Label44.Text)) >= 0 Then
Label47.Text = ((Val(runup) + Val(Label46.Text)) + Val(Label45.Text)).ToString("N2")
runup = 0
End If
End If
Catch ex As Exception
End Try
TxtBox5.Text = Label47.Text
是什么导致了较大数字的问题?
【问题讨论】:
-
那是很多代码,不清楚你的问题出在哪里。在我看来,您可以将代码减少到少于 5 行来说明问题。请问可以吗?
-
我试过减少它,但它没有用......最初的问题是去除小数点“.60456”之后的十进制值并将其四舍五入到第 60 位,就像我们以前一样小数点前加一到60,我已经做到了。
-
现在我的新问题是如果数字超过 999.60,而不是我的结果是 1000.00,它会读取 1.00。如果你能帮忙试试这些号码 600.30 和 600.20
-
请提供将这两个数字相加的代码(两个数字相加不需要 60 行代码),这说明了问题。
-
从设计的角度来看,最好从所有 GUI 代码中抽象出操作值的代码。编写一个简短的函数,尝试做你想做的事,并提出你的问题。作为一个额外的好处,一旦你让它工作,代码本身将更具可读性。请阅读此minimal reproducible example
标签: vb.net