Math, Financial, and the Boolean "Is" Functions

 

Mathematical Functions

 

VB.NET provides a wealth of mathematical functions that can be incorporated into your applications. Among them are the following:

 

Function

Returns:

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.

Rnd

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

 

The mathematical functions below are part of the System.Math namespace. Therefore, to use these, you must either place the statement Imports System.Math at the top of your code module, or you must qualify these functions with "Math." (i.e., "Math.Abs", "Math.Round", etc.). A small handful of the Math functions are shown below. In addition to those shown, there are many trigonometric and other functions available.

 

Function

Returns:

Abs(x)

the absolute value of x

Pow(x, y)

X raised to the power of y

Round(x, y)

x rounded to y decimal places

Sqrt(x)

the square root of x

 

 

How to Isolate the Decimal Portion of a Mixed Number with the Fix Function:

 

Dim sngMixedNumber As Single

Dim sngDecimalPortion As Single

 

sngDecimalPortion = sngMixedNumber – Fix(sngMixedNumber)

 

 

Generating Random Numbers with the Rnd Function:

 

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

(upperboundlowerbound + 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

 

VB.NET provides a number of financial functions that can be incorporated into your applications. The financial functions below are part of the Microsoft.VisualBasic.Financial namespace. Therefore, to use these, you must either place the statement Imports Microsoft.VisualBasic.Financial at the top of your code module, or you must qualify these functions with "Financial." (i.e.,"Financial.Pmt", "Financial.FV", etc.). 

 

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

 

Function

Description

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

IsDBNull

Returns a Boolean value that indicates whether an object (generally a database field) is null (contains missing or non-existent data).

IsNothing

Returns a Boolean value indicating whether an object variable currently has an object assigned to it.

IsArray

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