PowerShell supports the following arithmetic operators:
Operator
|
Describe
|
Example
|
Results
|
+
|
Add up two values
|
6+2
|
8
|
-
|
Subtract two values
|
6-2
|
4
|
-
|
Converts a value to a corresponding negative value
|
-2+6
|
4
|
*
|
Multiply two values
|
6*2
|
12
|
/
|
Divide two values
|
6/2
|
3
|
%
|
Returns the remainder of a division operation
|
6%4
|
2
|
Operator Precedence
There are a number of factors that determine how an expression containing arithmetic operators is handled. These factors include: the type of operator, the order of the operators, and whether there are expressions enclosed in parentheses. For example, 10+4/2 returns a result of 12, whereas (10+4)/2 Returns the result is 7. In the first example, you first deal with 4/2 and then add it to 10. In the second example, 10+4 is processed first and then sentenced to 2.
PowerShell calculates an arithmetic operator based on the following precedence rules:
1. (Process negative R)
2. *,/,%
3. +,-(processing subtraction)
Based on the above priority relationship, PowerShell the expression from left to right. For example, 3+6/3*4 returns the result 11. First 6 divided by 3, the result equals 2. Using 2 times 4, the result is 8. Add the result to 3 and get the final result 11.
You can change the precedence of an operator by enclosing parentheses around the specified expression. The parts enclosed in parentheses will be evaluated first and then evaluated for other parts. For example, (3+3)/(1+1) Returns a result of 3. First, compute the 3+3 and then count the 1+1. The first part of the result is 6, then divided by the second part of the result 2, the final result is 3.
Arithmetic operators and variables
Arithmetic operators are often used with variables. For example, suppose the $inta variable is assigned a value of 6, and the variable $INTB is assigned a value of 4. You can add operations to two variables using the + operator, as shown in the following command:
$intTotal = $intA + $intB
In this example, 6 and 4 are added. The result is 10, assigning the result to the variable $inttotal. An equal sign (=) is used to assign a value to a variable $inttotal.
We can think of PowerShell as a calculator. Enter the math expression as you type the command line, and the PowerShell automatically calculates and outputs the result. Common subtraction modulo (+,-, *,/,%) operations and parentheses expressions are supported.
PS c:\pstest> 1+2+3
6
PS c:\pstest> 0xABCD
43981
PS c:\pstest> 3.14*10*10
and PS C:\ Pstest> 1+3-(2.4-5) * (7.899-4.444)
12.983
PowerShell can also automatically identify computer capacity units, including KB,MB,GB,TB,PB
PS c:\pstest> 1PB/1TB
1024
PS c:\pstest> 1tb/1gb
1024
PS c:\pstest> 1gb/1kb
1048576
PS c:\pstest> 1gb/20mb*10kb
524288
If a website each page size is 80kb, the statistic shows PV operation for 800, 1 months down to occupy the bandwidth:
PS c:\pstest> 80KB*800*30/1GB
1.8310546875
If a website's per capita PV operation is 5, the page size is 80Kb, the host provider limit total traffic is 10G, that average daily maximum number of visitors
For:
PS c:pstest> 10gb/(80kb*5)/30
873.813333333333