Stripping Decimals Without Rounding

by Feb 10, 2012

When you divide numbers and just want the decimals before the decimal point, you could cast the result to integer. However, this would also round the result:

PS> 18 / 5
3.6
PS> [Int](18/5)
4

To strip off all decimals behind the decimal point, use Truncate() from the .NET Math library:

PS> [Math]::Truncate(18/5)
3

Likewise, to manually round, use Floor() or Ceiling().

Twitter This Tip! ReTweet this Tip!