RSS

How to get the integer part of a given number

08 Mar

In Visual Basic we could use Fix() and Int() to get the integer part of a given numeric value. But they handle negative values differently, I’ll simply show some examples as follows:

number Fix(number) Int(number)
12.2 12 12
11.8 11 11
-9.3 -9 -10
-9.8 -9 -10
-10.2 -10 -11

Here is the code to compare both function:

Private Sub TestGetIntegerPart()
    Dim numbers() As Double = {12.2, 11.8, -9.3, -9.8, -10.2}

    Dim dt As New DataTable
    dt.Columns.Add("number")
    dt.Columns.Add("Fix")
    dt.Columns.Add("Int")
    Dim row As DataRow

    For Each num As Double In numbers
        row = dt.NewRow()
        row("number") = num
        row("Fix") = Fix(num)
        row("Int") = Int(num)

        dt.Rows.Add(row)
    Next

    Me.DataGridView1.DataSource = dt
End Sub
Advertisements
 
Leave a comment

Posted by on March 8, 2012 in .NET, VB

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: