Is It Numeric?


How to check out if the string variable contains numbers only and can be converted to the numeric variable? Just try to parse it! The TryParse method converts a string variable to a number and returns a boolean value to indicate if the conversion has been successful.

Example 1

$Input = “12345”
$Result = 0
$IsNumeric = [System.Int32]::TryParse($Input, [ref]$Result)

In this case $IsNumeric will be $True and $Result will be 12345

Example 2

$Input = “12AS345”
$Result = 0
$IsNumeric = [System.Int32]::TryParse($Input, [ref]$Result)

In this case $IsNumeric will be $False and $Result will not be change, so will be 0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: