PowerShell supports the following arithmetic operators:
Operators
|
describe
|
example
|
result
|
+
|
Add two values together
|
6+2
|
8
|
-
|
Subtract two values
|
6-2
|
4
|
-
|
Convert the value to the corresponding negative value
|
-2+6
|
4
|
*
|
Multiply two values
|
6*2
|
12
|
/
|
Divide two values
|
6/2
|
3
|
%
|
Returns the remainder of the division operation
|
6%4
|
2
|
Operator priority
There are some factors that determine how expressions containing arithmetic operators are processed. These factors include: operator type, operator order, and whether expressions are enclosed in parentheses. For example, 10+4/2 returns result 12, but (10+4)/2 returns result 7. In the first example, 4/2 is first processed, and then added to 10. In the second example, 10+4 is first processed, and then 2 is given.
PowerShell calculates arithmetic operators according to the following priority rules:
1. (Treat negative number r)
2. *, /, %
3. +, - (Treat the subtraction)
According to the above priority relationship, PowerShell processes expressions from left to right. For example, 3+6/3*4 returns result 11. First divide 6 by 3, the result equals 2. Multiply 2 by 4, and the result is 8. Add the result to 3 to get the final result 11.
Enclosing the expression you specified in brackets can change the operator priority. The part enclosed in brackets will be evaluated first, and then the other parts will be evaluated. For example, (3+3)/(1+1) returns the result as 3. First, calculate 3+3, and then count 1+1. The first part is 6, then divide by the result 2 of the second part, and the final result is 3.
Arithmetic operators and variables
Arithmetic operators are often used with variables. For example, suppose the $intA variable is assigned to 6 and the variable $intB is assigned to 4. You can add two variables using the + operator, as shown in the following command:
$intTotal = $intA + $intB
In this example, 6 and 4 are added together. The result is 10, and the result is assigned to the variable $intTotal. The equal sign (=) is used to assign a value to the variable $intTotal.
We can treat powershell as a calculator. Enter a mathematical expression like typing the command line, enter, powershell will automatically calculate and output the result. Commonly used addition, subtraction, multiplication and division (+,-,*,/,%) operations and bracket expressions are supported.
PS C:\pstest> 1+2+3 6 PS C:\pstest> 0xABCD 43981 PS C:\pstest> 3.14*10*10 314 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 the page size of a website is 80kb, statistics show that the PV operation per day is 800, and the bandwidth occupied in one month:
PS C:\pstest> 80kb*800*30/1gb 1.8310546875
If a website's per capita PV operation is 5 per day, the page size is 80Kb, and the total traffic limit of the host provider is 10G, then the average maximum number of visitors per day
for:
PS C:pstest> 10GB/(80KB*5)/30 873.813333333333