VBScript Number Formatting Functions

Advertisements


Syntax :

variablename = Format_function_Name(Expression[,NumberDigAfterDec[,LeadingDig[,
UseParForNegNum[,GroupDigits]]]])

Description

  1. The Required parameter Format_function_Name corresponds to any of the below listed number formatting functions.

  2. The Optional parameter Expression corresponds to any numerical expression, which would result in a number.

  3. The Optional parameter NumberDigAfterDec corresponds to the number of digits after the decimal place.

  4. The Optional parameter LeadingDig corresponds to whether or not a leading zero is displayed for fractional values. It takes one of the three values based on the below settings parameter.

  5. The Optional parameter UseParForNegNum corresponds to whether or not to place negative values within parentheses. It takes one of the three values based on the below settings parameter.

  6. The Optional parameter GroupDigits corresponds to whether or not numbers are grouped using the group delimiter. It takes one of the three values based on the below settings parameter.

Settings

The above parameters LeadingDig, UseParForNegNum and GroupDigits arguments can have any of the following settings:

  • -2 = vbUseDefault - Use the computer's regional settings

  • -1 = vbTrue - True

  • 0 = vbFalse - False

The following table shows the possible number formatting functions in VBScript. Number Formatting Functions help the developers to handle numbers in an efficient way and express the numbers in the desired format.

FunctionDescription
FormatNumberA Function, which would return an expression formatted as a number
FormatPercentA Function, which would return an expression formatted as a percentage

Example :

Try the following example to understand all the Number Formatting Functions available in VBScript.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

Dim num : num = -645.998651

document.write("Line 1 : " & FormatNumber(num, 3))& "<br/>"

' The UseParensForNegativeNumbers parameter is set to true.
document.write("Line 2 : " & FormatNumber (num, 3, , vbTrue))&" <br/> "

' The GroupDigits parameter is set to false.
document.write("Line 3 : " & FormatNumber (num, 3, , , vbFalse)) & "<br/>"

document.write("Line 4 : " & FormatPercent(num, 3))& "<br/>"

' The UseParensForNegativeNumbers parameter is set to true.
document.write("Line 5 : " & FormatPercent (num, 3, , vbTrue))&" <br/> "

' The GroupDigits parameter is set to false.
document.write("Line 6 : " & FormatPercent (num, 3, , , vbFalse)) & "<br/>"

</script>
</body>
</html>

When executed the above script, following is the output:

Line 1 : -645.999
Line 2 : (645.999) 
Line 3 : -645.999
Line 4 : -64,599.865%
Line 5 : (64,599.865%) 
Line 6 : -64599.865%


Advertisements
Advertisements