PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Forum experts available to answer your PowerShell questions

How convert string to int?

rated by 0 users
This post has 1 Reply | 2 Followers

Top 100 Contributor
Posts 27
Georgiy Posted: 12-02-2009 2:24 AM

I'm need convert string type to int and  backwards. How can I do it?

Top 25 Contributor
Posts 511

You could cast it to [int] and [string] (or use ToString method). You might find about_Variables help topic (http://technet.microsoft.com/en-us/library/dd347604.aspx) interesting, especially TYPES OF VARIABLES section.

PS>$str = "123"

PS>$str.GetType().FullName

System.String

PS>$int = [int]$str

PS>$int.GetType().FullName

System.Int32

 

PS>$int = 456

PS>$int.GetType().FullName

System.Int32

PS>$str = $int.ToString()

PS>$str.GetType().FullName

System.String

-or-

PS>$str2 = [string]$int

PS>$str2.GetType().FullName

System.String

 

-aleksandar

http://powershellers.blogspot.com

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.