In Powershell what is the idiomatic way of converting a string to an int?

前端 未结 7 648
栀梦
栀梦 2020-12-05 12:32

The only method I have found is a direct cast:

> $numberAsString = \"10\"
> [int]$numberAsString
10

Is this the standard approach in

7条回答
  •  死守一世寂寞
    2020-12-05 13:26

    Building up on Shavy Levy answer:

    [bool]($var -as [int])
    

    Because $null is evaluated to false (in bool), this statement Will give you true or false depending if the casting succeeds or not.

提交回复
热议问题