Do you need to round to the nearest whole number in PowerShell? Whether you’re working with file sizes, calculations, or system metrics, converting decimal numbers to clean whole numbers is always required.
In this tutorial, I’ll show you exactly how to round numbers to the nearest whole number in PowerShell using five different methods that cover every scenario you’ll encounter. From basic mathematical rounding to specialized techniques for always rounding up or down, you’ll learn the most effective approaches that I use regularly in enterprise environments.
Method 1: Using [Math]::Round()
The best method for rounding numbers in PowerShell is the [Math]::Round() method. This method follows standard mathematical rounding rules, where numbers with decimal parts of 0.5 or greater round up, and those below 0.5 round down. I use this method in about 80% of my scripts because it’s reliable and follows expected mathematical conventions.
# Basic rounding examples
$number1 = 3.7
$number2 = 3.2
$number3 = 3.5
$rounded1 = [Math]::Round($number1) # Result: 4
$rounded2 = [Math]::Round($number2) # Result: 3
$rounded3 = [Math]::Round($number3) # Result: 4
Write-Output "3.7 rounds to: $rounded1"
Write-Output "3.2 rounds to: $rounded2"
Write-Output "3.5 rounds to: $rounded3"The [Math]::Round() method works by examining the first decimal place and applying standard rounding rules. When the decimal is exactly 0.5, it rounds to the nearest even number (banker’s rounding), which helps reduce rounding bias in large datasets.
Here is the exact output in the screenshot below:

This behavior is particularly useful in financial calculations where I need to maintain mathematical precision.
Check out PowerShell Round to 2 Decimal Places
Method 2: [Math]::Floor() – Always Round Down
Sometimes you need to always round down to the nearest whole number, regardless of the decimal value. The [Math]::Floor() method is perfect for this scenario. I frequently use this method when working with file sizes, memory calculations, or any situation where you can’t exceed a certain threshold.
# Floor rounding examples
$price1 = 99.99
$price2 = 25.01
$price3 = 50.99
$floored1 = [Math]::Floor($price1) # Result: 99
$floored2 = [Math]::Floor($price2) # Result: 25
$floored3 = [Math]::Floor($price3) # Result: 50
Write-Output "$price1 floors to: $floored1"
Write-Output "$price2 floors to: $floored2"
Write-Output "$price3 floors to: $floored3"The Floor method essentially “truncates” the decimal portion, always rounding toward negative infinity. This makes it particularly useful for resource allocation scenarios where exceeding a limit isn’t acceptable. In my experience managing server resources, this method ensures conservative estimates that won’t cause system overruns.
Check out PowerShell Greater Than or Equal
Method 3: [Math]::Ceiling() – Always Round Up
Conversely, when you need to always round up to the nearest whole number, [Math]::Ceiling() is your go-to method. This is incredibly useful for capacity planning, pagination calculations, or any scenario where you need to ensure adequate resources. I often use this when calculating the number of servers needed to handle a specific load.
# Ceiling rounding examples
$users1 = 100.1
$users2 = 299.9
$users3 = 500.01
$ceiling1 = [Math]::Ceiling($users1) # Result: 101
$ceiling2 = [Math]::Ceiling($users2) # Result: 300
$ceiling3 = [Math]::Ceiling($users3) # Result: 501
Write-Output "$users1 users requires: $ceiling1 licenses"
Write-Output "$users2 users requires: $ceiling2 licenses"
Write-Output "$users3 users requires: $ceiling3 licenses"The Ceiling method always rounds toward positive infinity, ensuring you never underestimate requirements. This conservative approach has saved me countless times in project planning where underestimating resources would have caused significant issues.
You can see the exact output in the screenshot below:

Read PowerShell Select-String Plus Next Lines
Method 4: [Math]::Truncate() – Remove Decimal Places
The [Math]::Truncate() method simply removes the decimal portion without any rounding logic. While technically not “rounding to the nearest whole number,” it’s a valuable method for scenarios where you want the integer portion only. I use this method when working with time calculations or when I need consistent behaviour regardless of the decimal value.
# Truncate examples
$time1 = 15.99
$time2 = 15.01
$time3 = -15.99
$truncated1 = [Math]::Truncate($time1) # Result: 15
$truncated2 = [Math]::Truncate($time2) # Result: 15
$truncated3 = [Math]::Truncate($time3) # Result: -15
Write-Output "$time1 truncates to: $truncated1"
Write-Output "$time2 truncates to: $truncated2"
Write-Output "$time3 truncates to: $truncated3"Truncate differs from Floor when dealing with negative numbers – it always rounds toward zero rather than toward negative infinity. This behavior makes it perfect for scenarios where you need consistent directional rounding regardless of the number’s sign.
Check out PowerShell Script to Delete Files Older Than 30 Days
Method 5: Using Type Conversion [int] – Quick and Dirty
For quick scripts or when you need maximum performance, PowerShell’s built-in type conversion can round numbers to whole integers. While not as flexible as the Math methods, the [int] conversion provides a fast way to truncate decimal places. I often use this method in loops where performance is critical.
# Type conversion examples
$value1 = 42.8
$value2 = 99.1
$value3 = -10.7
$converted1 = [int]$value1 # Result: 43 (rounds to nearest)
$converted2 = [int]$value2 # Result: 99
$converted3 = [int]$value3 # Result: -11
Write-Output "$value1 converts to: $converted1"
Write-Output "$value2 converts to: $converted2"
Write-Output "$value3 converts to: $converted3"The [int] conversion actually performs banker’s rounding similar to [Math]::Round(), but it’s faster for simple operations. However, be cautious with large numbers as integer conversion has limits and can cause overflow errors in extreme cases.
Read Convert a String to a Binary Number in PowerShell
PowerShell Round to Nearest Whole Number Examples
Now, let me show you some examples that I have used mostly while working for various clients.
File Size Calculations
When working with file systems, I frequently need to convert bytes to more readable units while maintaining whole numbers:
# Convert bytes to MB and round
$fileSizeBytes = 1048576.7
$fileSizeMB = [Math]::Round($fileSizeBytes / 1MB, 0)
Write-Output "File size: $fileSizeMB MB"Performance Counter Calculations
In system monitoring scripts, CPU percentages often need rounding for cleaner reports:
# Round CPU usage percentages
$cpuUsage = 85.6789
$roundedCPU = [Math]::Round($cpuUsage)
Write-Output "CPU Usage: $roundedCPU%"Capacity Planning
When calculating server requirements, I always round up to ensure adequate resources:
# Calculate required servers
$totalUsers = 1247.5
$usersPerServer = 500
$requiredServers = [Math]::Ceiling($totalUsers / $usersPerServer)
Write-Output "Servers needed: $requiredServers"Conclusion
In this tutorial, I explained five methods to round to the nearest whole number in PowerShell. With examples, we saw how to use [Math]::Floor() and [Math]::Ceiling(), etc methods.
You may also like the following tutorials:
- Convert Base64 String to Byte Array in PowerShell
- Convert String to Base64 in PowerShell
- Convert String to Double in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.