在比较两个double类型时,我注意到以下行为。
给定以下基本脚本:
[double]$maxValue = 1.5
[double]$startValue = 1.2
[double]$counter = $startValue
while ($counter -lt $maxValue) {
Write-Host $counter
$counter += 0.1
}
输出:
1.2
1.3
1.4
如果我将while语句改为使用小于等于符号: while ($counter -le $maxValue) {
输出:
1.2
1.3
1.4
这段代码与上面的代码完全相同,因此在结尾处缺少了预期的“1.5”值。
如何在PowerShell中正确比较两个双精度数?