Using System.Math

While PowerShell itself comes with reasonably basic mathematical operators, the .NET class System.Math has a far wider variety.

The Round static method can be used to round up to a fixed number of decimal places. In the following example, the value is rounded to two decimal places:

[Math]::Round(2.123456789, 2) 

The Ceiling and Floor methods are used when performing whole-number rounding:

[Math]::Ceiling(2.1234)    # Returns 3 
[Math]::Floor(2.9876)      # Returns 2 

The Abs converts a positive or negative integer to a positive integer (multiplies by -1 if the value is negative):

[Math]::Abs(-45748) 

Numbers may be raised to a power:

[Math]::Pow(2, 8) # Returns 256 (28) 

A square root can be calculated:

[Math]::Sqrt(9)    # Returns 3 

The System.Math class contains static properties for mathematical constants:

[Math]::pi    # π, 3.14159265358979 
[Math]::e     # e, 2.71828182845905 

Methods are also available to work with log, tan, sin, cos, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229