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

前端 未结 14 2138
轮回少年
轮回少年 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 09:57

    You can check whether the variable is a number like this: $val -is [int]

    This will work for numeric values, but not if the number is wrapped in quotes:

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

提交回复
热议问题