Round fractions for long decimal numbers in PowerShell Script
Yes, It's possible to format the decimal numbers in PowerShell to show only specified numbers after fractions for long decimal numbers.
Round in PowerShell
- You can use
[math]::round()
to round up and round down the long decimal numbers in PowerShell.
[math]::round()
is not only round the decimal number to an integer number, but it also can round the fraction number for a specified length.
Ex (1): Round down decimal number to integer in PowerShell
The below example will automatically round the decimal number down to (480) due to the fraction part (.333333333333) is lower than 5.
PS C:\Users\spfarm> $decimal = 1441/3
PS C:\Users\spfarm> $decimal
480.333333333333
PS C:\Users\spfarm> [math]::round($decimal)
480
Ex (2): Round up decimal number to a whole number in PowerShell
The below example will automatically round the decimal number up to (412) due to the fraction part (.714285714286) is greater than or equal to 5.
PS C:\Users\spfarm> $decimal = 1441/3.5
PS C:\Users\spfarm> $decimal
411.714285714286
PS C:\Users\spfarm> [math]::round($decimal)
412
Ex (3): Round fraction part in a decimal number in PowerShell
The below example will automatically round the first two fractions in a decimal number
Due to the third number (4) in the fraction part (.714) is lower than 5, therefore the result will be rounded down to (411.71).
PS C:\Users\spfarm> $decimal = 1441/3.5
PS C:\Users\spfarm> $decimal
411.714285714286
PS C:\Users\spfarm> [math]::round($decimal,2)
411.71
Ex (4):
The below example will automatically round the first four fractions in a decimal number
Due to the fifth number (8) in the fraction part (.71428) is greater than or equal to 5, therefore the result will be rounded up to (411.7143).
PS C:\Users\spfarm> $decimal = 1441/3.5
PS C:\Users\spfarm> $decimal
411.714285714286
PS C:\Users\spfarm> [math]::round($decimal,4)
411.7143
> You can specify how many numbers to be shown after fraction like this [math]::round($decimal,n)
.
[int] vs [Round] in PowerShell
[Int] in PowerShell
[Int]
in PowerShell acts as [math]::round()
but
- It only shows integer number without fractions,
- You can't control the roundup and down for the fractions part.
- Additionally, you can't specify the numbers that you need to show after fractions!
Ex (1):
The below example only shows the integer number without fraction rounded up or down based on the fraction part value.
Due to the fraction part value (.714285714286) is greater than or equal to 5, therefore the result will be rounded up to (412).
PS C:\Users\spfarm> $decimal = 1441/3.5
PS C:\Users\spfarm> $decimal
411.714285714286
PS C:\Users\spfarm> [int]($decimal)
412
Ex (2):
The below example only shows the integer number without fraction rounded up or down based on the fraction value.
Due to the fraction part value (.333333333333) is lower than 5, therefore the result will be rounded down to (480).
PS C:\Users\spfarm> $decimal = 1441/3
PS C:\Users\spfarm> $decimal
480.333333333333
PS C:\Users\spfarm> [int]($decimal)
480