Saturday, 12 November 2011

Show-NumberPadding1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of leading Zeros 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates several numbers of varying type, then 
  7.     displays them using .NET Formatting. The second set 
  8.     of formatting shows the difference between .ToString() 
  9.     and composite format strings to format - to approaches 
  10.     that accomplish the same goal! 
  11. .NOTES 
  12.     File Name  : Show-NumberPadding1.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://pshscripts.blogspot.com/2011/11/show-numberpadding1ps1.html 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library/dd260048.aspx 
  20. .EXAMPLE 
  21.     Psh> Show-NumberPadding1.ps1 
  22.                   00000254               000000FE 
  23.                   00010342               00002866 
  24.                   01023983               000F9FEF 
  25.                   06985321               006A9669 
  26.        9223372036854775807       7FFFFFFFFFFFFFFF 
  27.       18446744073709551615       FFFFFFFFFFFFFFFF 
  28.      
  29.                   00000254               000000FE 
  30.                   00010342               00002866 
  31.                   01023983               000F9FEF 
  32.        9223372036854775807       7FFFFFFFFFFFFFFF 
  33.       18446744073709551615       FFFFFFFFFFFFFFFF        
  34.  
  35. #> 
  36.  
  37. [byte]   $byteValue   = 254 
  38. [int16]  $shortValue  = 10342 
  39. [int]    $intValue    = 1023983 
  40. [long]   $lngValue    = 6985321 
  41. [long]   $lngValue2   = [System.Int64]::MaxValue 
  42. [UInt64] $ulngValue   = [System.UInt64]::MaxValue 
  43.  
  44. # Display integer values by caling the ToString method. 
  45. "{0,22} {1,22}" -f $byteValue.ToString("D8") , $byteValue.ToString("X8"
  46. "{0,22} {1,22}" -f $shortValue.ToString("D8"), $shortValue.ToString("X8"
  47. "{0,22} {1,22}" -f $intValue.ToString("D8")  , $intValue.ToString("X8"
  48. "{0,22} {1,22}" -f $lngValue.ToString("D8")  , $lngValue.ToString("X8"
  49. "{0,22} {1,22}" -f $lngValue2.ToString("D8") , $lngValue2.ToString("X8"
  50. "{0,22} {1,22}" -f $ulngValue.ToString("D8") , $ulngValue.ToString("X8"
  51. "" 
  52.  
  53. # Display the same integer values by using composite formatting 
  54. "{0,22:D8} {0,22:X8}" -f $byteValue 
  55. "{0,22:D8} {0,22:X8}" -f $shortValue 
  56. "{0,22:D8} {0,22:X8}" -f $intValue 
  57. "{0,22:D8} {0,22:X8}" -f $lngValue2 
  58. "{0,22:D8} {0,22:X8}" -f $ulngValue 

No comments: