Table of Contents
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:
- Initialize a variable with the floating point number
- Round or format the number using the specific method
- 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.
1 2 3 4 5 |
$ActualNumber = 3.1448 $formattedNumber = $ActualNumber.ToString("F2") Write-Output $formattedNumber |
1 2 3 |
3.14 |
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.
1 2 3 4 5 |
$ActualNumber = 3.14159 $formattedNumber = "{0:N2}" -f $ActualNumber Write-Output $formattedNumber |
1 2 3 |
3.14 |
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.
1 2 3 4 5 |
$ActualNumber = 3.14159 $roundedNumber = [Math]::Round($ActualNumber, 2, [MidpointRounding]::ToEven) Write-Output $roundedNumber |
1 2 3 |
3.14 |
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.
1 2 3 4 5 |
$ActualNumber = 3.14159 $roundedDownNumber = [Math]::Floor($ActualNumber * 100) / 100 Write-Output $roundedDownNumber |
1 2 3 |
3.14 |
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.
1 2 3 4 5 |
$ActualNumber = 3.14159 $roundedUpNumber = [Math]::Ceiling($ActualNumber * 100) / 100 Write-Output $roundedUpNumber |
1 2 3 |
3.15 |
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.
1 2 3 4 5 |
$ActualNumber = 3.14159 $truncatedNumber = [Math]::Truncate($ActualNumber * 100) / 100 Write-Output $truncatedNumber |
A
1 2 3 |
3.14 |
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 theCeiling()
function that returned3.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.