Decimal Number Comparison


I’m retrieving directory sizes to 2 decimal places in GB:

$DirectoryGet = Get-ChildItem -Path $DirectoryPath -Recurse
$DirectorySize = [Math]::Round((($DirectoryGet | Measure-Object -Property Length -Sum).Sum /1GB),2)

Then grouping them into:

If ($DirectorySize -lt 1)
    <do something with directories under 1GB>
    <do something with directories greater than or equal to 1GB>

$DirectorySize.GetType() is a [double]. I’ve also tried it with [float] and [decimal] and all 3 seems to work in the if statement.

Is the above comparison actually comparing decimal numbers (eg $DirectorySize 0.23 < 1 or $DirectorySize 1.54 >= 1)?

Or is it just comparing the integer component of $DirectorySize left of the dot and ignoring everything right of the dot?


Would it make any difference in your case actually? :man_shrugging:t4: :smirk:

Regardless of that … do you have any reason to doubt? You mentioned that it works as expected? What’s your issue? :thinking:

And BTW:

Since you compare against an integer anyway one decimal point would have been enough though.

No issue. Just curious … for a deeper dive into mechanics of floating point comparison in powershell.

Code was just an ad hoc example. Google-fu brought up more questions than answers for me so thought I’ll tap the expertise over here.