Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
11.8k views
in PowerShell by 1 1 3
edited by

Is it possible to show only two numbers after fractions for long decimals in PowerShell Script?

PowerShell Script Example

PS C:\Users\romans> $d= 1441/3
PS C:\Users\romans> $d
480.333333333333

I want to format the decimal number in PowerShell to display only two numbers after fraction like this format #.## and the result should be 480.33


1 Answer

1 like 0 dislike
by 163 204 403
selected by
 
Best answer

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

round decimal numbers in PowerShell
> 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!

Round vs Int In PowerShell

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
by 1 1 3
1 0
You are the best, thank you.
by 163 204 403
0 0
Most Welcome, Glad to hear it helped you
If you don’t ask, the answer is always NO!
...