开发者

Comparison question string vs int

开发者 https://www.devze.com 2023-01-01 07:38 出处:网络
Can someone explain to me why these comparisons work they way the do. I had a bug in one of my scripts that took me a little bit to work through. I was using read-host and typing a nu开发者_如何学Cmbe

Can someone explain to me why these comparisons work they way the do. I had a bug in one of my scripts that took me a little bit to work through. I was using read-host and typing a nu开发者_如何学Cmber. It was storing it as a string.

Write-Host "(`'2`' -gt 9 ) = " ('2' -gt 9 )
Write-Host "(2 -gt 9 ) = " (2 -gt 9 )
Write-Host "(`'2`' -gt 10 ) = " ('2' -gt 10 )

If you are comparing a string to an Int does it use the Ascii value? If so why does the first one show $false, it should be $true.

Then how is it when you change the int to 10 it becomes $true.


On comparison a right value is converted to the type of a left value. Thus, '2' -gt 9 becomes '2' -gt '9', that is False, and '2' -gt 10 becomes '2' -gt '10', that is True.

0

精彩评论

暂无评论...
验证码 换一张
取 消