Saturday, 31 July 2010

New-BigInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and displays a BigInteger.  
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN sample. 
  6. .NOTES 
  7.     File Name  : New-BigInteger.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10.                  .NET Framework 4    
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/07/new-bigintegerps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx  
  16. .EXAMPLE 
  17.     PSH [c:\foo]: .\Get-BigInteger.ps1 
  18.     Big Integer from 179032.6541: 
  19.     179,032 
  20.     Big Integer from 1234934157136952: 
  21.     1,234,934,157,136,952 
  22. #> 
  23.  
  24. # Add the .NET Version 4 System.Numerics.DLL 
  25. Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll" 
  26.  
  27. # Create first big integer then display it nicely 
  28. $BigIntFromDouble = New-Object System.Numerics.BigInteger 179032.6541 
  29. "Big Integer from 179032.6541:" 
  30. $BigIntFromDouble.ToString("N0"
  31.  
  32. #Create second big integer then display it nicely 
  33. $BigIntFromInt64 = New-object System.Numerics.BigInteger 1234934157136952 
  34. "Big Integer from 1234934157136952:" 
  35. $Bigintfromint64.tostring("N0"

2 comments:

Epicurus said...

Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll' or one of its dependencies.

This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Thomas Lee said...

You are most probably using PowerShell V2 with the standard .NET 2.0 framework. The class being used here is new in the .NET Framework 4.0, thus is not normally available.

You can setup PowerShell to bind to .NET 4.0, in which case you'll find this assembly, alternatively, use the beta of PowerShell V3.