I am new to VB I am a c++ programmerr. I know in c++ when you declare a variable as an int it will return a whole number. In VB I am delaring a variable as an interger and when I do my calculations it comes back with a decimal. How can I truncate the number and just show the whole number??

Recommended Answers

All 4 Replies

Could you show your code for the calculation? Integer does return a whole number, so you must be using it with a single. VB will convert the integer to a single and return a single. This is one reason you should use Option Strict, so VB won't do any conversions, you have to do them yourself.

Oh, yes your question.... Cint(11.1111) will return 11. CInt(11.999) will return 12...etc.
If you just want the 11 then Fix(11.111) and Fix(11.999) will return 11.

This is what I need. I have text box and the user will enter a number it could be a decimal or whole number. From here I need to do some calulations and get only the whole number. Truncate the rest. I tried the strict option then my tryparse from double to int wont work.

I just need to get the whole number.

Well, parsing from double to integer may cause a loss on the data (OverflowException).
Assume we have a button on the form called btnDisplay
and TextBox called txtBox1 .

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim x As Integer
        x = Int32.Parse(txtBox1.Text)
        MessageBox.Show(x)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.