0

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??

3
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by StrikerX11
0

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.

0

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.

0

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.

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.