VB.net中的除法运算自动四舍五入

Division arithmetic in VB.net automatically rounding off

我对以下代码有一点问题。

        Dim k As Integer

    k = (TextBox1.Text - 750) \ 250

    MsgBox("There should be " & k & " items")

假设 textbox1 的值为 3050,结果将是 9.20,但是我的消息框 returns 9 不是我想要的。其实我想看没有四舍五入的

我该怎么做?

\是VB中的整数除法,所以需要用/代替。

有关 VB 运算符的详细信息,请参阅此处 (MSDN Documentation)。

此外,如评论中所述,您将 k 存储为整数 - 请改用 double(或其他)。

那么,怎么样:

Dim k As Double
Dim tbText as Double
If Double.TryParse(TextBox1.Text, tbText) Then
    k = (tbText  - 750) / 250

    MsgBox("There should be " & k & " items")
End If

如果您确定 TextBox1.Text 将是一个数字,您不必像我一样使用 TryParse它 - 但我不要认为您应该相信用户会做对...