In PowerShell, how can I test if a variable holds a numeric value?

前端 未结 14 2053
轮回少年
轮回少年 2020-12-16 09:15

In PowerShell, how can I test if a variable holds a numeric value?

Currently, I\'m trying to do it like this, but it always seems to return false.

相关标签:
14条回答
  • 2020-12-16 10:03

    You can do something like :

    $testvar -match '^[0-9]+$'
    

    or

    $testvar -match '^\d+$'
    

    Returns True if $testvar is a number.

    0 讨论(0)
  • 2020-12-16 10:05

    I ran into this topic while working on input validation with read-host. If I tried to specify the data type for the variable as part of the read-host command and the user entered something other than that data type then read-host would error out. This is how I got around that and ensured that the user enters the data type I wanted:

    do
        {
        try
            {
            [int]$thing = read-host -prompt "Enter a number or else"
            $GotANumber = $true
            }
        catch
            {
            $GotANumber = $false
            }
        }
    until
        ($gotanumber)
    
    0 讨论(0)
  • 2020-12-16 10:05
    "-123.456e-789" -match "^\-?(\d+\.?\d*)(e\-?\d+)?$|^0x[0-9a-f]+$"
    

    or

    "0xab789" -match "^\-?(\d+\.?\d*)(e\-?\d+)?$|^0x[0-9a-f]+$"
    

    will check for numbers (integers, floats and hex).

    Please note that this does not cover the case of commas/dots being used as separators for thousands.

    0 讨论(0)
  • 2020-12-16 10:07
    $itisint=$true
    try{
     [int]$vartotest
    }catch{
     "error converting to int"
     $itisint=$false
    }
    

    this is more universal, because this way you can test also strings (read from a file for example) if they represent number. The other solutions using -is [int] result in false if you would have "123" as string in a variable. This also works on machines with older powershell then 5.1

    0 讨论(0)
  • 2020-12-16 10:11

    -is and -as operators requires a type you can compare against. If you're not sure what the type might be, try to evaluate the content (partial type list):

    (Invoke-Expression '1.5').GetType().Name -match 'byte|short|int32|long|sbyte|ushort|uint32|ulong|float|double|decimal'
    

    Good or bad, it can work against hex values as well (Invoke-Expression '0xA' ...)

    0 讨论(0)
  • 2020-12-16 10:14

    Testing if a value is numeric or a string representation of a numeric value.

    function Test-Number 
    {
        Param
        (
            [Parameter(Mandatory=$true,
                       Position=0)]
            [ValidatePattern("^[\d\.]+$")]
            $Number
        )
    
        $Number -is [ValueType] -or [Double]::TryParse($Number,[ref]$null)
    }
    

    Testing if a value is numeric.

    function Test-Number 
    {
        Param
        (
            [Parameter(Mandatory=$true,
                       Position=0)]
            [ValidatePattern("^[\d\.]+$")]
            $Number
        )
    
        $Number -is [ValueType]
    }
    
    0 讨论(0)
提交回复
热议问题