Learn VB6's Trig, Math, Financial, Boolean, and Random functions



Math Functions





the absolute value of x


the trigonometric arctangent of x (in radians)


the trigonometric cosine of x (in radians)


exponential function ex


the integer portion of x


the integer portion of x, except that if x is negative, it will return the next smallest number.

For example, Int(-4.3) would return –5, not –4 as you might expect. To get the integer portion of a number, the Fix function will always produce the expected result.


natural logarithm of x (base e)

Round(x, y)

x rounded to y decimal places


a random number less than 1 but greater than or equal to zero


-1 if x is negative, 0 if x is 0, 1 if x is positive


the trigonometric sine of x (in radians)


the square root of x


the trigonometric tangent of x (in radians)


Tip: How to isolate the decimal portion of a mixed number with Fix:


Dim sngMixedNumber As Single

Dim sngDecimalPortion As Single


sngDecimalPortion = sngMixedNumber – Fix(sngMixedNumber)



Generating Random Numbers with Rnd:


Use the following formula to generate a random number between a range of numbers (where a range of numbers has a lowerbound and an upperbound – for example, in the range of 1 to 10, 1 is the lowerbound and 10 is the upperbound):

(upperbound – lowerbound + 1) * Rnd + lowerbound


The Rnd function returns a Single number between 0 and 1 (including 0, but not including 1); therefore, the other factors in the expression are used for scaling the range for the desired lowerbound and upperbound values.


For example, to generate a random number between 5 and 25.999999, you can use the expression:

(25 – 5 + 1) * Rnd + 5


which can be simplified to:

21 * Rnd + 5


To store the result in the variable sngRandomNumber, you could write the statement:

sngRandomNumber = 21 * Rnd + 5


Often, you only want to generate a random integer within a certain range. To accomplish this, use the VB Int function "on top of " the expression involving Rnd. The Int function truncates the decimal portion of the expression without rounding – unlike CInt, which does round. The following statements cause a random integer between 1 and 10 to be stored in the variable intRandNum:


intRandomNumber = Int((10 – 1 + 1) * Rnd + 1)

- or simply -

intRandomNumber = Int(10 * Rnd + 1)


IMPORTANT: Always execute the Randomize statement at least once in the program prior to using Rnd function, otherwise, the same set of random numbers will be generated on every execution of the program.



Financial Functions


To do this:

Use one of these functions:

Calculate depreciation.


Calculate future value.


Calculate interest rate.


Calculate internal rate of return.


Calculate number of periods.


Calculate payments.

IPmt, Pmt, PPmt

Calculate present value.




Boolean "Is" functions





Returns a Boolean value indicating whether an expression can be converted to a date. Useful for validating input.


Returns a Boolean value indicating whether an expression can be evaluated as a number. Can be useful for validating input; but use with caution: if the expression contains the letter "E" or "D", the input argument could be interpreted as a number in scientific notation (thus returning True when you would expect the function to return False).


Returns a Boolean value that indicates whether an expression contains no valid data (Null). Usually used with database fields, although a Variant variable can also contain Null (no other intrinsic VB datatype can store Null values).


Returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.


Returns a Boolean value indicating whether a Variant variable has been initialized (i.e., if it has ever been assigned a value).


Returns a Boolean value indicating whether a Variant variable represents an object.


Returns a Boolean value indicating whether a Variant variable contains the special value Error.


Returns a Boolean value indicating whether a variable is an array.

If you enjoyed this post, subscribe for updates (it's free)

does not divied

how do you do "does not divide by X"?
like i know in python you do %
for example:

if X % 2 then
end if


If I understand you right, I think you can use MOD where X MOD Y returns the remainder when X is divided by Y. If X can be divided by Y, it returns 0 (that is zero). For example;
10 MOD 3 = 1
5 MOD 3 = 2
6 MOD 3 = 0.

Your example can be stated as:

If X MOD 2 <> 0 Then
X = X * 3
End If

God bless you.

SLN Function.

Hi! I enjoy very much your page. I'm working with the depreciation function SLN in VB6, but I get
an error #5, checked with the 3 arguments, don't know what I'm doing wrong. Can u help me?

declaring finance functions in visual basic

I require the declaration syntax of finance functions and the tutorials for the same.