VBScript Functions

VBScript Format Functions
We have 4 primary built-in functions are there in vbscript
 
1) FormatCurrency Function

Description: 

Returns an expression formatted as a currency value using the currency symbol defined in the system control panel.

Examples:

Dim xCurrency
xCurrency = FormatCurrency(123)
Msgbox xCurrency ‘Output is: $123.00

Dim xCurrency
xCurrency = FormatCurrency(“50”)
Msgbox xCurrency ‘Output is: $50.00

Negative input
Dim xCurrency
xCurrency = FormatCurrency(“abc”)
Msgbox xCurrency ‘Error

2) FormatDateTime FunctionDescription: 

Returns an expression formatted as a date or time.

Syntax:

FormatDateTime(Date[, NamedFormat])

Example (1):

Dim CurrentDate
CurrentDate = FormatDateTime(Date, 0) ‘0 for Shot date
Msgbox CurrentDate ‘formats Date in Short Format (Ex: 1/18/2012)
Dim CurrentDate

Example (2):
CurrentDate = FormatDateTime(Date, 1) ‘1 for Long Date
Msgbox CurrentDate ‘formats Date in long date (Ex: Wednesday, January18, 2012)

Example (3):
Dim CurrentDate
CurrentDate = FormatDateTime(Date, 3)
Msgbox CurrentDate ‘Display a time using the time format specified in your computer’s regional settings (Ex: 12:00 AM)

Example (4):

3) FormatNumber Function 

Description: 
Returns an expression formatted as a number.

Example:

Dim MyAngle, MySecant, MyNumber
MyAngle = 1.3   ‘ Define angle in radians.
MySecant = 2 / Cos(MyAngle)   ‘ Calculate secant.
FormatNumberDemo = FormatNumber(MySecant,4) ‘ Format MySecant to four decimal places.
Msgbox FormatNumberDemo ‘Output is: 7.4767

4) FormatPercent Function

 Description:
Returns an expression formatted as a percentage (multiplied by Hundred) with a trailing % character.

Example:

Dim MyVal
MyVal = FormatPercent(5/32)
Msgbox MyVal ‘Output is: 15.63%
———————————————————-

 

 

Comments