VBScript FormatNumberFunction


VBScript 参考手册Complete VBScript Reference

The FormatNumber function returns an expression formatted as a number.

Syntax

FormatNumber(Expression[,NumDigAfterDec[,
IncLeadingDig[,UseParForNegNum[,GroupDig]]]])

Parameters Description
expression Required. The expression to be formatted.
NumDigAfterDec Optional. A value indicating how many digits are displayed to the right of the decimal point. The default value is -1 (the computer's regional settings are used).
IncLeadingDig Optional. Indicates whether leading zeros are displayed for fractional values:
  • -2 = TristateUseDefault - Use the computer's regional settings
  • -1 = TristateTrue - True
  • 0 = TristateFalse - False
UseParForNegNum Optional. Indicates whether negative values are placed in parentheses:
  • -2 = TristateUseDefault - Use the computer's regional settings
  • -1 = TristateTrue - True
  • 0 = TristateFalse - False
GroupDig Optional. Indicates whether numbers are grouped using the digit grouping symbol specified in the computer's regional settings:
  • -2 = TristateUseDefault - Use the computer's regional settings
  • -1 = TristateTrue - True
  • 0 = TristateFalse - False

Examples

Example 1

<script type="text/vbscript">

document.write(FormatNumber(20000))

</script>

The output of the above example is:

20,000.00

Try it yourself »

Example 2

Set the number of digits after the decimal point:

<script type="text/vbscript">

document.write(FormatNumber(20000,2) & "<br />")
document.write(FormatNumber(20000,5))

</script>

The output of the above example is:

20,000.00
20,000.00000

Try it yourself »

Example 3

Whether to display leading zeros for fractional values:

<script type="text/vbscript">

document.write(FormatNumber(.20,,0) & "<br />")
document.write(FormatNumber(.20,,-1))

</script>

The output of the above example is:

.20
0.20

Try it yourself »

Example 4

Whether to place negative values in parentheses:

<script type="text/vbscript">

document.write(FormatNumber(-50,,,0) & "<br />")
document.write(FormatNumber(-50,,,-1))

</script>

The output of the above example is:

-50.00
(50.00)

Try it yourself »

Example 5

Whether to group digits:

<script type="text/vbscript">

document.write(FormatNumber(1000000,,,,0) & "<br />")
document.write(FormatNumber(1000000,,,,-1))

</script>

The output of the above example is:

1000000.00
1,000,000.00

Try it yourself »

VBScript 参考手册Complete VBScript Reference Other Extensions