Format Number to 2 Decimal Places in PowerShell

Format number to 2 decimal places in PowerShell

In PowerShell, there are so many methods to round or format the numbers to two decimal points. In the following methods, the following steps will be followed:

  1. Initialize a variable with the floating point number
  2. Round or format the number using the specific method
  3. Display the Rounded or formatted number on the screen

Using ToString() Method with F2 Format Specifier

Use the ToString() method with the F2 format specifier to format numbers to two decimal places in PowerShell.

A PowerShell variable $ActualNumber is initialized with the floating point number that contains 4 decimal digits. Next, the ToString() function is called with F2 that format the number.

The F2 is a format specifier that specifies the value to the 2 decimal as its name implies F for Fixed-point and 2 for two digits. Finally, the output is printed on the console.

Using -f Operator with 0:N2 Format Specifier

Use the -f operator with the 0:N2 format specifier to format numbers to two decimal places in PowerShell.

In PowerShell, the -f is the format operator that applies the specified format to the following value. For example, the N2 is a format specifier like F2 that truncates the value of the floating point number to two decimal points.

Using Math.Round Class

Use the Math.Round class to format numbers to two decimal places in PowerShell.

In the Math class, the Round() method is used to format $ActualNumber, which contains the floating point number and is rounded to 2 decimal places.

The first parameter is the $ActualNumber variable, and the second parameter, 2, specifies the number of decimal places to round to. The third parameter, [MidpointRounding]::ToEven, specifies the rounding method. If the fractional part of the number is exactly 0.5, it will round to the nearest even integer.

Using Math.Floor Class

Use the Math.Floor class to format numbers to two decimal places in PowerShell.

The Floor() function rounds the given floating point number to the lowest near number. The variable $ActualNumber is given to the Floor() function as a parameter. The number is first multiplied by 100, rounded down to the nearest integer; after that, the number is divided by 100 to get the original number rounded down to two decimal places.

Using Math.Ceiling Class

Use the Math.Ceiling class to format numbers to two decimal places in PowerShell.

The Ceiling() function rounds the given floating point number to the highest near number. The variable $ActualNumber is given to the Ceiling() function as a parameter.

The number is first multiplied by 100 and rounded up to the nearest integer; after that, the number is divided by 100 to get the original number rounded down to two decimal places.

Using Math.Truncate Class

Use the Math.Truncate class to format numbers to two decimal places in PowerShell.

A

The Truncate() function rounds the given floating point number. The variable $ActualNumber is given to the given function as a parameter. The number is multiplied by 100, which removes the decimal part, then divided by 100 again to get the original value truncated to two decimal places.

Note that all the methods above generated the same output, i.e., 3.14 except the Ceiling() function that returned 3.15. Therefore, the only difference between the methods is how they handle rounding up or down.

That’s all about how to format number to 2 decimal places in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *