Export (0) Print
Expand All

Conversion.Fix Method (Int32)

Return the integer portion of a number.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static int Fix(
	int Number
)

Parameters

Number
Type: System.Int32

Required. A number of type Double or any valid numeric expression. If Number contains Nothing, Nothing is returned.

Return Value

Type: System.Int32
Return the integer portion of a number.

ExceptionCondition
ArgumentNullException

Number is not specified.

ArgumentException

Number is not a numeric type.

Both the Int and Fix functions remove the fractional part of Number and return the resulting integer value.

The difference between Int and Fix functions is that if Number is negative, Int returns the first negative integer less than or equal to Number, whereas Fix returns the first negative integer greater than or equal to Number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.

Fix(number) is equivalent to Sign(number) * Int(Abs(number)).

This example illustrates how the Int and Fix functions return integer portions of numbers. In the case of a negative number argument, the Int function returns the first negative integer less than or equal to the number; the Fix function returns the first negative integer greater than or equal to the number. The following example requires you to specify Option Strict Off because implicit conversions from type Double to type Integer are not allowed under Option Strict On:

' This code requires Option Strict Off 
Dim MyNumber As Integer
MyNumber = Int(99.8)   ' Returns 99.
MyNumber = Fix(99.8)   ' Returns 99.

MyNumber = Int(-99.8)  ' Returns -100.
MyNumber = Fix(-99.8)  ' Returns -99.

MyNumber = Int(-99.2)  ' Returns -100.
MyNumber = Fix(-99.2)  ' Returns -99.

You can use the CInt function to explicitly convert other data types to type Integer with Option Strict Off. However, CInt rounds to the nearest integer instead of truncating the fractional part of numbers. For example:

MyNumber = CInt(99.8)    ' Returns 100.
MyNumber = CInt(-99.8)   ' Returns -100.
MyNumber = CInt(-99.2)   ' Returns -99.

You can use the CInt function on the result of a call to Fix or Int to perform explicit conversion to integer without rounding. For example:

MyNumber = CInt(Fix(99.8))   ' Returns 99.
MyNumber = CInt(Int(99.8))   ' Returns 99.

For more information on CInt, see Type Conversion Functions (Visual Basic).

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft