VBScript Number Formatting Functions


Advertisements

Syntax

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

Description

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

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

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

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

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

  • 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

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%
vbscript_numbers.htm
Advertisements