github.com/coveo/gotemplate@v2.7.7+incompatible/docs/doc_test/arithmetic.razor (about) 1 {% include navigation.html %} 2 {% raw %} 3 # Basic mathematic operations 4 5 ## Operators mixin 6 7 Note that you cannot combine razor extended expression (+, -, /, *, etc.) with go template expression such as in: 8 9 | Razor expression | Go Template | Result | Note 10 | ------------------------------- | ------------------------------------ | -----: | ---- 11 | @(2 + (2 | mul(4))); | {{ add 2 (bor 2 (mul 4)) }} | 8 | In this statement, | is interpreted as bitwise or between 2 and 4 12 | @(sum 2 (2 | mul 4)); | {{ sum 2 (2 | mul 4) }} | 10 | While in this statement (no binary operator), | is interpreted as go template piping operator 13 14 ## Addition 15 16 | Razor expression | Go Template | Result | Note 17 | ---------------------- | --------------------------- | -----: | ---- 18 | {{ add 1 2 }} | {{ add 1 2 }} | 3 | **Addition** 19 | {{ add 4 5 }} | {{ add 4 5 }} | 9 | *or add* 20 | {{ sum 6 7 }} | {{ sum 6 7 }} | 13 | *or sum* 21 | {{ add 2 3 }} | {{ add 2 3 }} | 5 | Spaces are optional 22 | {{ add 8 9 }} | {{ add 8 9 }} | 17 | You can insert an arbitrary number of spaces in expressions 23 | {{ sum 1.2 3.4 }} | {{ sum 1.2 3.4 }} | 4.6 | It also works with floating point numbers 24 | {{ sum 1 2 3 4 }} | {{ sum 1 2 3 4 }} | 10 | It is possible to supply multiple arguments to addition operation 25 | {{ add (list 1 2 3 4) }} | {{ add (list 1 2 3 4) }} | 10 | this is useful on this line since there is ambiguity on where the expression finish 26 27 ## Subtraction 28 29 | Razor expression | Go Template | Result | Note 30 | ---------------------- | --------------------------- | -----: | ---- 31 | {{ sub 4 2 }} | {{ sub 4 2 }} | 2 | **Subtraction** 32 | {{ sub 4 2 }} | {{ sub 4 2 }} | 2 | *or sub* 33 | {{ subtract 4 2 }} | {{ subtract 4 2 }} | 2 | *or subtract* 34 35 ## Negative values 36 37 | Razor expression | Go Template | Result | Note 38 | ---------------------- | --------------------------- | -----: | ---- 39 | {{ -23 }} | {{ -23 }} | -23 | Negative value 40 | {{ add 2 -23 }} | {{ add 2 -23 }} | -21 | Operation with negative value 41 | {{ add 2 (sub 0 (mul 5 3)) }} | {{ add 2 (sub 0 (mul 5 3)) }} | -13 | Operation with negative expression 42 43 ## Product 44 45 | Razor expression | Go Template | Result | Note 46 | ---------------------- | --------------------------- | -----: | ---- 47 | {{ mul 2 3 }} | {{ mul 2 3 }} | 6 | **Multiplication** 48 | {{ mul 4 5 }} | {{ mul 4 5 }} | 20 | *or mul* 49 | {{ multiply 6 7 }} | {{ multiply 6 7 }} | 42 | *or multiply* 50 | {{ prod 8 9 }} | {{ prod 8 9 }} | 42 | *or prod* 51 | {{ product 10 11 }} | {{ product 10 11 }} | 110 | *or product* 52 | {{ mul 1 2 3 4 }} | {{ mul 1 2 3 4 }} | 24 | It is possible to supply multiple arguments to multiplication operation 53 | {{ mul (list 5 6 7 8) }} | {{ mul (list 5 6 7 8) }} | 1680 | or even an array 54 55 ## Division 56 57 | Razor expression | Go Template | Result | Note 58 | ---------------------- | --------------------------- | -----: | ---- 59 | {{ div 4 2 }} | {{ div 4 2 }} | 2 | **Division** 60 | {{ div 13 3 }} | {{ div 13 3 }} | 4.333333333333333 | *you can use the รท character instead of /* 61 | {{ div 20 4 }} | {{ div 20 4 }} | 5 | *or div* 62 | {{ divide 10 4 }} | {{ divide 10 4 }} | 2.5 | *or divide* 63 | {{ quotient 22 10 }} | {{ quotient 22 10 }} | 2.2 | *or quotient* 64 65 ## modulo 66 67 | Razor expression | Go Template | Result | Note 68 | ---------------------- | --------------------------- | -----: | ---- 69 | {{ mod 4 3 }} | {{ mod 4 3 }} | 1 | **Modulo** 70 | {{ mod 12 5 }} | {{ mod 12 5 }} | 2 | *or mod* 71 | {{ modulo 20 6 }} | {{ modulo 20 6 }} | 2 | *or modulo* 72 73 ## Power 74 75 | Razor expression | Go Template | Result | Note 76 | ---------------------- | --------------------------- | -----: | ---- 77 | {{ power 4 3 }} | {{ pow 4 3 }} | 64 | **Power** 78 | {{ pow 12 5 }} | {{ pow 12 5 }} | 248832 | *or pow* 79 | {{ power 3 8 }} | {{ power 3 8 }} | 6561 | *or power* 80 | {{ pow10 3 }} | {{ pow10 3 }} | 1000 | **Power 10** 81 | {{ power10 5 }} | {{ power10 5 }} | 100000 | *or power10* 82 | {{ 1e+5 }} | {{ 1e+5 }} | 100000 | Scientific notation (positive) 83 | {{ 2e-3 }} | {{ 2e-3 }} | 0.002 | Scientific notation (negative) 84 85 ## Bit operators 86 87 | Razor expression | Go Template | Result | Note 88 | ------------------------ | --------------------------- | -----: | ---- 89 | {{ lshift 1 8 }} | {{ lshift 1 8 }} | 256 | **Left shift** 90 | {{ lshift 3 5 }} | {{ lshift 3 5 }} | 96 | *or lshift* 91 | {{ leftShift 4 4 }} | {{ leftShift 4 4 }} | 64 | *or leftShift* 92 | {{ rshift 1024 4 }} | {{ rshift 1024 4 }} | 64 | **Right shift** 93 | {{ rshift 456 3 }} | {{ rshift 456 3 }} | 57 | *or rshift* 94 | {{ rightShift 72 1 }} | {{ rightShift 72 1 }} | 36 | *or rightShift* 95 | {{ band 65535 512 }} | {{ band 65535 512 }} | 512 | **Bitwise AND** 96 | {{ band 12345 678 }} | {{ band 12345 678 }} | 32 | *or band* 97 | {{ bitwiseAND 222 111 }} | {{ bitwiseAND 222 111 }} | 78 | *or bitwiseAND* 98 | @(1 | 2 | 4); | {{ bor (bor 1 2) 4 }} | 7 | **Bitwise OR** 99 | {{ bor 100 200 300 }} | {{ bor 100 200 300 }} | 492 | *or bor* 100 | {{ bitwiseOR 64 256 4 }} | {{ bitwiseOR 64 256 4 }} | 324 | *or bitwiseOR 101 | {{ bxor (bxor 1 2) 4 }} | {{ bxor (bxor 1 2) 4 }} | 7 | **Bitwise XOR** 102 | {{ bxor 100 200 300 }} | {{ bxor 100 200 300 }} | 384 | *or bxor* 103 | {{ bitwiseXOR 64 256 4 }} | {{ bitwiseXOR 64 256 4 }} | 324 | *or bitwiseXOR 104 | {{ bclear 255 4 }} | {{ bclear 255 4 }} | - | **Bitwise Clear** 105 | {{ bclear 0xff 3 8 }} | {{ bclear 0xff 3 8 }} | - | *or bclear* 106 | {{ bitwiseClear 0xf 7 }} | {{ bitwiseClear 0xf 7 }} | - | *or bitwiseClear 107 108 ## Other mathematic functions 109 110 ### Special cases 111 112 There are special behavior for certain operators depending of the arguments: 113 114 #### String multiplication 115 116 {{ mul "*" 100 }} will result in {{ mul "*" 100 }} which result in: 117 118 **************************************************************************************************** 119 120 #### Math operation on array 121 {% endraw %}