In PowerShell, how can I test if a variable holds a numeric value? -
in powershell, how can test if variable holds numeric value?
currently, i'm trying this, seems return false
.
add-type -language csharpversion3 @' public class helpers { public static bool isnumeric(object o) { return o byte || o short || o int || o long || o sbyte || o ushort || o uint || o ulong || o float || o double || o decimal ; } } '@ filter isnumeric($inputobject) { [helpers]::isnumeric($inputobject) } ps> 1 | isnumeric false
modify filter this:
filter isnumeric { [helpers]::isnumeric($_) }
function
uses $input
variable contain pipeline information whereas filter
uses special variable $_
contains current pipeline object.
edit:
for powershell syntax way can use filter (w/o add-type):
filter isnumeric() { return $_ -is [byte] -or $_ -is [int16] -or $_ -is [int32] -or $_ -is [int64] ` -or $_ -is [sbyte] -or $_ -is [uint16] -or $_ -is [uint32] -or $_ -is [uint64] ` -or $_ -is [float] -or $_ -is [double] -or $_ -is [decimal] }
Comments
Post a Comment