O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

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

Syntax

Fix(number)
number

Use: Required

Data Type: Numeric

Any valid numeric expression.

Return Value

The same data type as passed to the function containing only the integer portion of number.

Description

Removes the fractional part of a number. Operates in a similar way to the Int function.

Rules at a Glance

  • If number is Null, Fix returns Null.

  • The operations 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).

Example

   Dim dblTest
   Dim varTest

   dblTest = -100.9353
   varTest = Fix(dblTest)
   ' returns -100
  Msgbox  varTest & " " & TypeName(varTest) 

   dblTest = 100.9353
   varTest = Fix(dblTest)
   'returns 100
   Msgbox.Print varTest & " " & TypeName(varTest)

Programming Tips and Gotchas

Fix doesn’t round number to the nearest whole number; it simply removes the fractional part of number. Therefore, the integer returned by Fix is the nearest whole number less than (or greater than, if the number is negative) the number passed to the function.

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