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

前端 未结 14 2090
轮回少年
轮回少年 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条回答
  •  萌比男神i
    2020-12-16 09:54

    If you want to check if a string has a numeric value, use this code:

    $a = "44.4"
    $b = "ad"
    $rtn = ""
    [double]::TryParse($a,[ref]$rtn)
    [double]::TryParse($b,[ref]$rtn)
    

    Credits go here

提交回复
热议问题