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

前端 未结 14 2088
轮回少年
轮回少年 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:16

    filter isNumeric {
        $_ -is [ValueType]
    }
    

    -

    1 -is [ValueType]
    True
    "1" -is [ValueType]
    False
    

    -

    function isNumeric ($Value) {
        return $Value -is [ValueType]
    }
    
    isNumeric 1.23
    True
    isNumeric 123
    True
    isNumeric ""
    False
    isNumeric "asdf123"
    False
    

    -

    (Invoke-Expression '1.5') -is [ValueType]
    

提交回复
热议问题