Powershell – Measure-Object is not exact because of rounding/flooring

In case you want to get maximal value without rounding/flooring – to be exact you should not use Measure-Object. You need to enumerate array (which is also faster).

Example:

[decimal]$a="1.60000000000000000008" 
[decimal]$b="1.60000000000000000009" 
$a, $b | Measure-Object -Maximum
Result: Maximum : 1,6

Which is not what we wanted. For this we need to build own array and enumerate it:

$arr = @(
    "1.60000000000000000008",
    "1.60000000000000000009"
)

$max = ""
foreach ($item in $arr) {
    if ($item -gt $max) {
        $max = $item
    }
}
$max

Which results into:

1.60000000000000000009

Another method could be:

$arr | Sort-Object -Descending | Select-Object -First 1

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.