除法运算符总是返回一个整数

时间:2018-05-09 19:07:58

标签: vb.net visual-studio

我正在编写Windows窗体应用程序。使用变量Dim secnumber查看此代码的后半部分。当我运行代码并给它一个像53这样的值时,它只会舍入到整数,我希望它是十进制形式。我无法弄清楚我做错了什么。

    'Declare variable
    Dim number As Integer

    'Extract the number from the text box
    Dim intnumber = Convert.ToInt32(txtintbox.Text)

    'Calculate to double the number
    number = intnumber * 2

    'Convert the number to display in message box
    MessageBox.Show("When the number is doubled, the result is: " & number)

    'Clear the text box
    txtintbox.Text = ""

End Sub

Private Sub halfbtn_Click(sender As Object, e As EventArgs) Handles halfbtn.Click




    'Declare variable
    Dim secnumber As Double

    'use th number from the text box
    secnumber = Convert.ToInt32(txtintbox.Text)

    'Calculate the number by half
    secnumber = secnumber \ 2

    'Convert number to display the result in message box
    MessageBox.Show("Half of this number is: " & secnumber)

    'Clear the text box
    txtintbox.Text = ""


End Sub

1 个答案:

答案 0 :(得分:2)

secnumber = secnumber \ 2更改为secnumber = secnumber / 2

/是标准部门 \是整数除法

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/integer-division-operator