小数を整数に変換する

 小数を整数に直すのは非常に特徴のある動きをします。基本的には四捨五入なのですが、小数部分が .5 の場合、偶数になるように切り捨て・切り上げが行われます。

PS U:\PS> [int]1.3
1
PS U:\PS> [int]1.5
2
PS U:\PS> [int]2.5
2
PS U:\PS>

 普通に切り捨てを行いたい場合は数学用のクラスを使って次のように書きます。

PS U:\PS>
PS U:\PS> [math]::Truncate(1.1)
1
PS U:\PS> [math]::Truncate(1.5)
1
PS U:\PS> [math]::Truncate(1.9)
1
PS U:\PS> [math]::Truncate(2.0)
2
PS U:\PS>

 四捨五入を行いたい場合は、.5を加えて小数切り捨てを行います。

PS U:\PS>
PS U:\PS> [math]::Truncate(1.1+.5)
1
PS U:\PS> [math]::Truncate(1.5+.5)
2
PS U:\PS> [math]::Truncate(1.9+.5)
2
PS U:\PS> [math]::Truncate(2.0+.5)
2
PS U:\PS>
inserted by FC2 system