Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Working with decimals in an if / switch statement

$
0
0

Hi guys, 

I have been staring at my screen and Googling the life out of me, but I can't see why I can't get my statement to work. 

What I want to do:

$input = "7.5" 

$input

if ($input -eq "0") {Write-Host "none"}

if ($input -gt "0.1" -and $input -lt "2.5") {Write-Host "light"}

if ($input -gt "2.6" -and $input -lt "7.6") {Write-Host "medium"}

if ($input -gt "7.7" -and $input -lt "50") {Write-Host "heavy"}

if ($input -gt "50" -and $input -lt "100") {Write-Host "storm"}

 

My result is when I press F5 is:

it says that $input = 8... not 7.5 

I think it has something to do with it being a string. So if I do: [int]$input = "7.5" it makes no difference. Even if I do $input.GetType() it says its name = Int32

 

If I do:

$strInput= "7.5"

$intInput = $strInput -as [int32] 

It still round the 7.5 up to an 8!! Why!?!?! Even if I change "Int32" with "Decimal" I still get the same.. I must be missing something...

Hope you guys can help me out. 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles