ASP Math Function

Math functions are part of our ASP scripts. Mostly they are used within the applications and support the script with various features. We will discuss some common functions used frequently inside our code.

Int function to get Integer part
From any number we can get the integer part only by using int function . Here are some values returned by int function.
Int ( 4.0 ) will return  4 
Int ( 4.1 ) will return  4 
Int ( 4.9 ) will return  4 
Int ( 4.5 ) will return  4 
Int ( 1.1 ) will return  1 
Int ( 0 ) will return   0
Int ( -0.1 ) will return  -1 
Int ( -2.1 ) will return  -3 
Int ( -2.9 ) will return  -3 

Mod function to get the reminder of division

We can get the reminder after a division is by using mod function. Here is the code
Dim my_num1,my_num2
my_num1=50
my_num2=10
Response.Write my_num1 mod my_num2

The output of above line is 0. If we change the value of my_num1 to 55 then the output will change to 5.

ASP round function to format decimal place

Round function is used to round of a number and format the number as per requirement.
Round(5.768) will return 6
Round(5.368) will return 5
Round(0.768) will return 1
Round(-3.368) will return -3
We can format a number as per requirement of decimal place. For example we want the number should display two decimal places only.
Round(5.768,2) will return 5.77
The above line is rounded off to two decimal place.

ASP Random number generator

We can generate random number for our applications using ASP. The random number can be used for displaying random images, or random banners or random colors for a page or random numbers for any type of application. To generate random number we have to initiate random number generator first by using randomize function. Here is the code to generate random number between 1 and 10
Dim my_num
Randomize
my_num = Int((rnd*10))+1
Response.Write my_num
The function rnd returns random number between 0 and .9 so we have multiplied it by 10 to move the decimal place one step right. Then took the integer part by using Int function, then we have added 1 to it.

We can also give a range of number and generate random number between the given maximum and minimum numbers. Here is the code.
Dim my_num,max,min

max=5
min=2

Randomize
my_num=int((max-min+1)*rnd+min)
Response.Write my_num

ASP percentage value by using FormatPercent function

We can get the percentage of two numbers by using ASP function FormatPercent(). We can use this function to get the percentage and we can format the output as per our requirement. Here is the syntax of FormatPercent function.
FormatPercent(n1/n2,d3)
Here n1 and n2 are two variables and d3 is the number specifying number of decimal places required. d3 is an optional value we can specify as the number of decimal places we want. Default value for this is 2.

Here is an example of FormatPercent function.
Dim my_num
my_num=FormatPercent(11/203,6) ' Output is 5.418719%
Response.Write my_num
one more example without using optional value for decimal places
Dim my_num
my_num=FormatPercent(11/203) ' Output is 5.42%
Response.Write my_num

Be the first to post comment on this article :

plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer