Sunday 2 August 2009

Convert-Double.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script is an updated MSDN sample showing converstion to int32, boolean, string 
  4.     and char. The sample is written using PowerShell 
  5. .DESCRIPTION 
  6.     This script 
  7. .NOTES 
  8.     File Name  : convert-double.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.convert.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\convert-double.ps1' 
  18.     Converting 23.15 to an int32 = 23 
  19.     Converting 23.15 to an Boolean = True 
  20.     Converting 23.15 to a String = 23.15 
  21.     Converting 1st char of "23.15" to a char = 2 
  22. #> 
  23.  
  24. ## 
  25. # Start of sample 
  26. ## 
  27.   
  28. # Create double to convert 
  29. [system.double] $dNumber = 23.15 
  30.  
  31. # convert to int32 
  32. [int32] $iNumber = [System.Convert]::ToInt32($dNumber
  33. "Converting $dnumber to an int32 = $inumber" 
  34.   
  35. # convert to bool 
  36. [bool] $bNumber = [System.Convert]::ToBoolean($dNumber); 
  37. "Converting $dnumber to an Boolean = $bNumber" 
  38.   
  39. # convert to string 
  40. [string] $strNumber = [System.Convert]::ToString($dNumber
  41. "Converting $dnumber to a String = $strNumber" 
  42.   
  43. # convert a single char in the string to a char 
  44. [char] $chrNumber = [System.Convert]::ToChar($strNumber[0]); 
  45. "Converting 1st char of `"$strnumber`" to a char = $chrNumber" 
  46. # End of script 

No comments: