Name

Fix Function

Class

Microsoft.VisualBasic.Conversion

Syntax

Fix(number)
number

Use: Required

Data Type: Double or any numeric expression

A number whose integer portion is to be returned

Return Value

A number of the same data type as number whose value is the integer portion of number

Description

For nonnegative numbers, Fix returns the floor of the number (the largest integer less than or equal to number).

For negative numbers, Fix returns the ceiling of the number (the smallest integer greater than or equal to number).

Rules at a Glance

  • If number is Nothing, Fix returns Nothing.

  • The operation of Int and Fix are identical when dealing with positive numbers: numbers are rounded down to the next lowest whole number. For example, both Int(3.14) and Fix(3.14) return 3.

  • If number is negative, Fix removes its fractional part, thereby returning the next greater whole number. For example, Fix(-3.667) returns -3. This contrasts with Int, which returns the negative integer less than or equal to number (or -4, in the case of our example).

  • The function returns the same data type as was passed to it.

Example

Sub TestFix(  )
    
   Dim dblTest As Double
   Dim objTest  As Object
    
   dblTest = -100.9353
   objTest = Fix(dblTest)
   ' returns -100
   Console.WriteLine(objTest & " " & TypeName(objTest)) 
    
   dblTest = 100.9353
   objTest = Fix(dblTest)
   'returns 100
   Console.WriteLine(objTest & " " & TypeName(objTest))

End Sub

Programming Tips and Gotchas

Fix does not round number to the nearest whole number; it simply removes the fractional ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.