2

The following code prints It's greater than 1:

$value = "1.9964672441318374e-005"
If ($value -ge 1)
{
    Write-Host "It's greater than 1"
}
Else
{
    Write-Host "Smaller than 1"
}

I understand that this can be corrected by casting $value to a [double]. I was wondering if anyone else encountered this inconsistent behavior. I say inconsistent because for decimal formatted strings the comparison yields the correct output. Any thoughts?

1 Answer 1

2

The quotes make the variable be treated as a string:

PS> $v=1.1e3
PS> $v |get-member #-> TypeName : System.Double     

PS> $v="1.1e3"
PS> $v |get-member #-> TypeName : System.String  

If you remove the quotes on in your original code, you'll find that the comparison works the way that you would expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.