locked
Powershell Comparing numbers RRS feed

  • Question

  • Hi,

    When I try to compare variable with a number , some times it gives false result. for example

    $number = 40

    If($number -gt 250)

    {

    Write-host " test"

    }

    Sometime it works but some time looks like it compares first 2 numbers and gives the result, ie 25 is not greater than 40. I tried '250' and "250" but its not working.  Please correct me here.

    Tuesday, September 6, 2016 9:57 PM

Answers

  • if ([int]$size -gt $num)

    You cannot compare a string to a number correctly.  $size is a string because you created it as a string.

    This would be best:

    if($objFSO.GetFolder($i.FullName).Size -gt 500Gb){


    \_(ツ)_/

    • Proposed as answer by FWN Wednesday, September 7, 2016 3:37 PM
    • Marked as answer by SaveEarthGreen Wednesday, September 7, 2016 8:32 PM
    Wednesday, September 7, 2016 3:21 PM

All replies

  • PS> $number = 40
    PS> $number.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Int32                                    System.ValueType
    
    PS> '250'.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object
    

    It is not possible to compare numbers with strings

    Greetings,
    (79,108,97,102|%{[char]$_})-join''

    Tuesday, September 6, 2016 10:03 PM
  • This is nt a string issue.

    The left side of the comparison is an Int32 so the right will be "coerced" into an Int32 even if it is a string..  You would only be right if the left side was a string.

    These two are different

    $number = 40
    $number = '40'

    They will also compare differently.

    PS D:\scripts> 40 -gt 250
    False
    PS D:\scripts> '40' -gt 250
    True
    PS D:\scripts>


    \_(ツ)_/

    Tuesday, September 6, 2016 10:46 PM
  • OK. So it is technically possible, but it does not make sense  ... at least for me. ;-)

    Thanks
    Greetings,
    (79,108,97,102|%{[char]$_})-join''

    Wednesday, September 7, 2016 1:06 AM
  • OK. So it is technically possible, but it does not make sense  ... at least for me. ;-)

    Thanks
    Greetings,
    (79,108,97,102|%{[char]$_})-join''

    It is the foundation of all programmin languages.  It is the foundation of the universe and the physics and chemistry of the universe.

    All of the chemistry of the universe is "left" dominant.  The majority of all molecules in the universe are "left" molecules.

    Remember that LSD is L-Lysergic Acid.  It is a left molecule.  L-Dopa is a left molecule.  The universe and all of nature always choose the "left".  What is not to understand.

    We invented a replacement for aspirin called Celebrex.  It was created by choosing an analog for aspirin that was a filtered version using only left handed molecules.

    So why would you doubt this?

    To be more serious this actually came from mathematics.  Formulas are evaluated left to right and the left factor determines the context.  Programmers designing compilers in the 1950s chose to follow this example and give priority for type to the left most member of a formula being evaluated by a compiler.  This has stuck in all languages that I know of.

    It is not a thing you need to have some theological belief in.  It is just a rule.

    Simple if you try it:

    PS D:\scripts> 40 + '1'
    41
    PS D:\scripts> '40' + 1
    401
    It is how things work in computers.  This is why all technicians need to learn the fundamentals of computers and computer science.  A failure to learn he fundamentals of computer science will show up in your paycheck - I can guarantee it.


    \_(ツ)_/

    Wednesday, September 7, 2016 1:19 AM
  • Hi,

    >>Sometime it works but some time looks like it compares first 2 numbers and gives the result, ie 25 is not greater than 40. I tried '250' and "250" but its not working.  Please correct me here.

    I have test this  issue on my ps2.0 &3.0,5.0 version, found no issues.

    Please try to restart the ISE or Powershell  Console then try again or trying to upgrade your .net framework or ps version to see if it helps.

    Best regards,

    Andy_Pan


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft..com.

    Wednesday, September 7, 2016 3:42 AM
  • It depends how the $number variable is populated. If the code before is not well written, sometimes $number could be something different than a number. You need to compare numbers, which You may test like this:

    $number = 40

    $number | gm (should return TypeName: System.Int32)

    if you want to enforce that $number has to be a single number, write it like this: [int]$number = 40

    Note that [int] enforces WHOLE number, so if not whole number it rounds automatically.

    [int]$number = 40.7

    $number

    41

    If you need more help provide part of the code / results given.


    Please remember to "Mark as answer" if it helps. Thank You.

    Wednesday, September 7, 2016 7:45 AM
  • Hi,

    Still getting false results. not sure whats wrong here.


    $num = 500
    $startFolder = "C:\test\test1
    $objFSO = New-Object -com  Scripting.FileSystemObject
    $colItems = Get-ChildItem $startFolder
    foreach ($i in $colItems)
        {
      
    $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object length -sum)

     $size ="{0:N2}" -f (($objFSO.GetFolder($i.FullName).Size) / 1GB)

     if ($size -gt $num)
     
     {

    $smtp.Send($msg)
     
     }

    Wednesday, September 7, 2016 3:17 PM
  • if ([int]$size -gt $num)

    You cannot compare a string to a number correctly.  $size is a string because you created it as a string.

    This would be best:

    if($objFSO.GetFolder($i.FullName).Size -gt 500Gb){


    \_(ツ)_/

    • Proposed as answer by FWN Wednesday, September 7, 2016 3:37 PM
    • Marked as answer by SaveEarthGreen Wednesday, September 7, 2016 8:32 PM
    Wednesday, September 7, 2016 3:21 PM