Converting VBScript's FormatNumber Function

Definition: Returns an expression formatted as a number.

FormatNumber

For most people, formatting a number simply means specifying the number of decimal points to be displayed. In Windows PowerShell you can use .NET formatting strings to specify the number of decimal places. The following example assigns the value 11 to the variable $a, then uses the .NET formatting string "{0:N6}" to format $a so that it displays 6 digits beyond the decimal point (that's what the 6 in the formatting string is for):

$a = 11
$a = "{0:N6}" -f $a

When you run this command and then echo back the value of $a you should get the following:

11.000000

Note. Windows PowerShell will round numbers up or down as needed to fit the new format. For example, suppose $a is equal to 11.54543 and you decide to format it with just a single decimal point. Windows PowerShell will assign the value 11.5 to $a (because the discarded decimal digits - 4543 - are rounded down to 5).

Incidentally, this formatting command will also insert the appropriate list separator. For example, try running these two commands and see what you get back:

$a = 11000000
$a = "{0:N6}" -f $a

When you echo back the value of $a (at least on a machine using the default setting for US English) you’ll get back the following value:

11,000,000.000000

Return to the VBScript to Windows PowerShell home page