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

Powershell

Powershell Problem Overview


The only method I have found is a direct cast:

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

Is this the standard approach in Powershell? Is it expected that a test will be done before to ensure that the conversion will succeed and if so how?

Powershell Solutions


Solution 1 - Powershell

You can use the -as operator. If casting succeed you get back a number:

$numberAsString -as [int]

Solution 2 - Powershell

Using .net

[int]$b = $null #used after as refence
$b
0
[int32]::TryParse($a , [ref]$b ) # test if is possible to cast and put parsed value in reference variable
True
$b
10
$b.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

note this (powershell coercing feature)

$a = "10"
$a + 1 #second value is evaluated as [string]
101 

11 + $a # second value is evaluated as [int]
21

Solution 3 - Powershell

A quick true/false test of whether it will cast to [int]

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

Solution 4 - Powershell

For me $numberAsString -as [int] of @Shay Levy is the best practice, I also use [type]::Parse(...) or [type]::TryParse(...)

But, depending on what you need you can just put a string containing a number on the right of an arithmetic operator with a int on the left the result will be an Int32:

PS > $b = "10"
PS > $a = 0 + $b
PS > $a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType

You can use Exception (try/parse) to behave in case of Problem

Solution 5 - Powershell

I'd probably do something like that :

[int]::Parse("35")

But I'm not really a Powershell guy. It uses the static Parse method from System.Int32. It should throw an exception if the string can't be parsed.

Solution 6 - Powershell

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.

Solution 7 - Powershell

$source = "number35"

$number=$null

$result = foreach ($_ in $source.ToCharArray()){$digit="0123456789".IndexOf($\_,0);if($digit -ne -1){$number +=$\_}}[int32]$number

Just feed it digits and it wil convert to an Int32

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn KaneView Question on Stackoverflow
Solution 1 - PowershellShay LevyView Answer on Stackoverflow
Solution 2 - PowershellCB.View Answer on Stackoverflow
Solution 3 - PowershellmjolinorView Answer on Stackoverflow
Solution 4 - PowershellJPBlancView Answer on Stackoverflow
Solution 5 - PowershellManu ClementzView Answer on Stackoverflow
Solution 6 - PowershellEdwardoView Answer on Stackoverflow
Solution 7 - PowershellHans van der SandeView Answer on Stackoverflow