Home > Software design >  rounding numbers powershell
rounding numbers powershell

Time:10-18

I want to make the code round the number if it crosses the number .4 For example 2.4 = 2, 2.5 = 3 That is, it does not round up to 4, but after it rounds

CodePudding user response:

Use MidpointRounding.AwayFromZero additional parameter:

[Math]::Round(2.5, [MidpointRounding]::AwayFromZero) # 3
[Math]::Round(0.5, [MidpointRounding]::AwayFromZero) # 1
[Math]::Round(2.49, [MidpointRounding]::AwayFromZero) # 2, because 2.49 is not midpoint

By default, [Math]::Round uses [MidpointRounding]::ToEven if not specified.

Please see docs for details.

  • Related