Monday 1 July 2013

Numeric Functions in VB.NET

Numeric Funcitons provides various function to be operated on integer or numeric variables to get Required result. Main Numeric Functions in VB.NET are :-


1. Round Function :-The Round function is used for rounding numbers to the number of digits you want. It will replace number to its most nearest by rounding off it

Syntax:-  
Round ( expression , precession)

expression  :- expression is value which Round function will round
precession :- It denotes to how many digits you want after decimal point 

Example :-

Dim num1 as integer
Dim num2 as integer
num1 = 10
num2 = 3
Dim precession as integer
Msgbox (round ( num1 / num2 , precession ))

2. Int and Fix Function :- 
Int Function is used to drop fractional part of a positive number .
Fix Function is used to drop fractional part of negative number

Syntax :-
Int ( +/-  num1 / num2 )
Fix ( +/-  num1 / num2 )

Example :-

Dim num1 as integer
Dim num2 as integer
num1 = 10
num2 = 3
Msgbox ( Int ( num1 / num2 ))
Msgbox ( Int ( - num1 / num2 )
Msgbox ( Fix ( num1 / num2 ))
Msgbox ( Fix ( - num1 / num2 ))

When Int is used with negative number , it returns the next lowest whole number.

3. Abs Function :- The Abs function returns the absolute value of a number. It means it remove the negative sign of number and it always returns the positive value.

Syntax :- 
Abs ( num1 )

Example :-

Dim num1 as integer
Dim num2 as integer
num1 = 10
num2 = -10
Msgbox ( Abs ( num1 ))
Msgbox ( Abs( num2 ))

4. Rnd Function :- Rnd Function returns a single precision , random numbers between specified value Range. 
Syntax :- 
Rnd * value

Exmple :-

Dim Random_number as integer
Random_number = Rnd * 5     ''It will produce Random numbers between 0 to 5
Msgbox ( Random_number )

5. Sgn Function :- This Function returns 1 for any positive number , -1 for any given negative number and 0 for zero number . 

Syntax :- 
Sgn ( number )

Example :-

Dim num1 as integer
Dim num2 as integer
Dim num3 as integer
num1 = 20
num2 = -20
num3 = 0
Msgbox ( sgn ( num1 ))
Msgbox ( sgn ( num2 ))
Msgbox ( sgn ( num3 ))

6. Sqr Function :- This Function returns the square root of number or specified number

Syntax :-
Sqr ( number )

Example :-

Dim num1 as integer
num1 = 10
Msgbox ( sqr ( num1))

7. Hex And Oct Function :- 
Hex Function returns hexadecimal equivalent of a decimal number 
Oct Function returns octal equivalent of decimal number

Example :-

Hex ( 12 )    ' returns a value C
Hex ( 2658  )    ' returns a value A62
Oct ( 19 )        ' returns a value 23
Oct ( 109 )        ' returns a value 135

8. Max Function :- Max Function is used several times in programming environment. Max Function returns maximum number from two numbers given as arguments to Max Function . This Max Function is applicanle for only two numbers not for more than two numbers 

Syntax :
Max ( number1 , number2 )

Example :-

Dim num1 as integer 
Dim num2 as integer 
num1 = 10
num2  = 20
Msgbox ( Max ( num1 , num2 ))    ' Returns 20

No comments:

Post a Comment