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

Level:

Returns:

Abs(x)

the absolute value of x

Atn(x)

the trigonometric arctangent of x (in radians)

Cos(x)

the trigonometric cosine of x (in radians)

Exp(x)

exponential function ex

Fix(x)

the integer portion of x

Int(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.

Log(x)

natural logarithm of x (base e)

Round(x, y)

x rounded to y decimal places

Rnd

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

Sgn(x)

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

Sin(x)

the trigonometric sine of x (in radians)

Sqr(x)

the square root of x

Tan(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. DDB, SLN, SYD Calculate future value. FV Calculate interest rate. Rate Calculate internal rate of return. IRR, MIRR Calculate number of periods. NPer Calculate payments. IPmt, Pmt, PPmt Calculate present value. NPV, PV

Boolean "Is" functions

IsDate

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

IsNumeric

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).

IsNull

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).

IsMissing

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

IsEmpty

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

IsObject

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

IsError

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

IsArray

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
x*3
end if

MOD

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?
Thanks!

declaring finance functions in visual basic

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

Post new comment

• Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
• Lines and paragraphs break automatically.
• You can enable syntax highlighting of source code with the following tags: `<code>`, `<blockcode>`. The supported tag styles are: `<foo>`, `[foo]`.