Stripping Decimals Without Rounding

by | February 10,2012

Table of Contents

Extracting the Integer Part of a Division Result

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

Removing Decimals Using Math.Truncate()

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().
 
ReTweet this Tip!