O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Fix Function

Class

Microsoft.VisualBasic.Conversion

Syntax

Fix(number)
number (required; 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 part ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required