Showing posts with label formatting. Show all posts
Showing posts with label formatting. Show all posts

Saturday, 12 November 2011

Show-NumberPadding3.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of double values. 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates a double value then formats it with 5 leading 
  7.     zeros. 
  8. .NOTES 
  9.     File Name  : Show-NumberPadding3.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/ 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/dd260048.aspx  
  17. .EXAMPLE 
  18.     Psh> Show-NumberPadding3.ps1 
  19.     01053240 
  20.     00103932.52 
  21.     01549230 
  22.  
  23.             01053240 
  24.          00103932.52 
  25.             01549230 
  26.        9034521202.93 
  27.  
  28. #>   
  29.  
  30. $fmt                 = "00000000.##" 
  31. [int]     $intValue  = 1053240 
  32. [decimal] $decValue  = 103932.52 
  33. [float]   $sngValue  = 1549230.10873992 
  34. [double]  $dblValue  = 9034521202.93217412 
  35.  
  36. # Display the numbers using the ToString method 
  37. $intValue.ToString($fmt) 
  38. $decValue.ToString($fmt) 
  39. $sngValue.ToString($fmt)            
  40. "" 
  41.  
  42. # Display the numbers using composite formatting 
  43. $formatString = " {0,15:" + $fmt + "}"  # right justified 
  44. $formatString -f $intValue  
  45. $formatString -f $decValue  
  46. $formatString -f $sngValue       
  47. $formatString -f $dblValue 

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