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

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

提交回复
热议问题