Export (0) Print
Expand All

Formatting Numbers

Microsoft® Windows® 2000 Scripting Guide

On its own, VBScript does not always format numbers in easy-to-read fashion. For example, one of the first scripts used in this chapter returned the free disk space on a hard drive. The value returned was 10842824704. This value is accurate but difficult to read; it would be much better to have the free space reported like this: 10,842,824,704.

Likewise, a calculation performed earlier in this chapter returned the value 10340.4458007813. Rarely, if ever, will you require that type of precision when performing system administration tasks. Instead, it would be better to have the value returned as 10,340 or 10,340.45. These numbers not only are easier to read but also require less room to display. This is an important consideration when creating tabular output to be displayed on the screen.

The FormatNumber function allows you to control how numbers are displayed in script output. FormatNumber requires you to specify the number to be formatted, followed by any or all of the parameters described in Table 2.16. For example, this line of code displays the number 37.874 with no decimal places (that is, the value 38 will be displayed):



Wscript.Echo FormatNumber(37.874, 0)

Table 2.16 FormatNumber Parameters

Parameter

Description

Number of digits after the decimal point.

FormatNumber does not simply delete digits. Instead, it rounds the number to the closest value. For example, if you decide to display 11.6 with no decimal places, the value 12 will be displayed. By contrast, 19.2 is displayed as 19.

If you use the value 1, the number of digits after the decimal point will be based on the regional and language options for the computer.

Use leading zero.

Values are:

-1 True. Use the leading 0.

0 False.

-2 Use the setting configured in the regional and language options for the computer.

Place negative numbers in parentheses.

Values are:

-1 True. Put negative numbers in parentheses.

0 False. Do not put negative numbers in parentheses.

-2 Use the setting configured in the regional and language options for the computer.

Group digits.

Values are:

-1 True. Group digits using the group separator.

0 False. Do not group digits.

-2 Use the setting configured in the regional and language options for the computer.

To help make your scripts easier to read and maintain, you might want to use constants in place of the literal values. For example, in this script it is not readily apparent what the 0 means:



Wscript.Echo FormatNumber(37.874, 0)

Using a constant makes the script easier to understand:



Const NoDecimalPlaces = 0
Wscript.Echo FormatNumber(37.874, NoDecimalPlaces)

Because each of the FormatNumber parameters is optional you, can perform such tasks as grouping digits without specifying values for the other parameters. However, the parameter for grouping digits must still be the fifth item in the parameter list (after the number to be formatted and the other three optional parameters). To ensure that the parameters maintain the correct order, you can do one of the following:

  • Use a "blank" parameter (that is, simply insert a comma without including a value).

  • Use the default value by setting each unused parameter to False.

  • Use the default value by setting each unused parameter to a constant that has been set to False.

For example, in this script constants are assigned the appropriate values, and then used to format the result of 1 / 7:



Const Decimals = 3
Const LeadingZero = True
Const UseParentheses = True
Const Default = False
NumberToFormat = 1 / -7
Wscript.Echo NumberToFormat
Wscript.Echo FormatNumber(NumberToFormat, Decimals)
Wscript.Echo FormatNumber(NumberToFormat, Default, LeadingZero)
Wscript.Echo FormatNumber(NumberToFormat, Default, Default, UseParentheses)

When the preceding script runs under CScript, the following output appears in the command window:



-0.142857142857143
-0.143
-0.14
(0.14)

The following script snippet shows different ways to format the value returned when multiplying 33 by 454:



Const GroupDigits = True
Const Default = False
Const NoDecimals = 0
NumberToFormat = 33 * 454
Wscript.Echo NumberToFormat
Wscript.Echo FormatNumber(NumberToFormat, Default, Default, Default, GroupDigits)
Wscript.Echo FormatNumber _
    (NumberToFormat, NoDecimals , Default, Default, GroupDigits)

When the preceding script runs under CScript, the following output appears in the command window:



14982
14,982.00
14,982

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft