Arithmetic

Arithmetic

Addition

Razor expressionGo TemplateResultNote
@(1 + 2);{{ add 1 2 }}3Addition
@add(4, 5);{{ add 4 5 }}9or add
@sum(6,7);{{ sum 6 7 }}13or sum
@(2+3);{{ add 2 3 }}5Spaces are optional
@( 8 + 9 );{{ add 8 9 }}17You can insert an arbitrary number of spaces in expressions
@sum(1.2, 3.4);{{ sum 1.2 3.4 }}4.6It also works with floating point numbers
@sum(1, 2, 3, 4);{{ sum 1 2 3 4 }}10It is possible to supply multiple arguments to addition operation
@add(list(1,2,3,4));{{ add (list 1 2 3 4) }}10this is useful on this line since there is ambiguity on where the expression finish

Subtraction

Razor expressionGo TemplateResultNote
@(4 - 2);{{ sub 4 2 }}2Subtraction
@sub(4, 2);{{ sub 4 2 }}2or sub
@subtract(4, 2);{{ subtract 4 2 }}2or subtract

Negative values

Razor expressionGo TemplateResultNote
@(-23);{{ -23 }}-23Negative value
@(2 + -23);{{ add 2 -23 }}-21Operation with negative value
@(2 + -(5 * 3));{{ add 2 (sub 0 (mul 5 3)) }}-13Operation with negative expression

Product

Razor expressionGo TemplateResultNote
@(2 * 3);{{ mul 2 3 }}6Multiplication
@mul(4, 5);{{ mul 4 5 }}20or mul
@multiply(6, 7);{{ multiply 6 7 }}42or multiply
@prod(8, 9);{{ prod 8 9 }}42or prod
@product(10, 11);{{ product 10 11 }}110or product
@mul(1, 2, 3, 4);{{ mul 1 2 3 4 }}24It is possible to supply multiple arguments to multiplication operation
@mul(list(5,6,7,8));{{ mul (list 5 6 7 8) }}1680or even an array

Division

Razor expressionGo TemplateResultNote
@(4 / 2);{{ div 4 2 }}2Division
@(13 ÷ 3);{{ div 13 3 }}4.333333333333333you can use the ÷ character instead of /
@div(20, 4);{{ div 20 4 }}5or div
@divide(10, 4);{{ divide 10 4 }}2.5or divide
@quotient(22, 10);{{ quotient 22 10 }}2.2or quotient

modulo

Razor expressionGo TemplateResultNote
@(4 % 3);{{ mod 4 3 }}1Modulo
@mod(12, 5);{{ mod 12 5 }}2or mod
@modulo(20, 6){{ modulo 20 6 }}2or modulo

Power

Razor expressionGo TemplateResultNote
@(4 ** 3);{{ pow 4 3 }}64Power
@pow(12, 5);{{ pow 12 5 }}248832or pow
@power(3, 8);{{ power 3 8 }}6561or power
@pow10(3);{{ pow10 3 }}1000Power 10
@power10(5);{{ power10 5 }}100000or power10
@(1e+5);{{ 1e+5 }}100000Scientific notation (positive)
@(2e-3);{{ 2e-3 }}0.002Scientific notation (negative)

Bit operators

Razor expressionGo TemplateResultNote
@(1 « 8);{{ lshift 1 8 }}256Left shift
@lshift(3, 5);{{ lshift 3 5 }}96or lshift
@leftShift(4, 4);{{ leftShift 4 4 }}64or leftShift
@(1024 » 4);{{ rshift 1024 4 }}64Right shift
@rshift(456, 3);{{ rshift 456 3 }}57or rshift
@rightShift(72, 1);{{ rightShift 72 1 }}36or rightShift
@(65535 & 512);{{ band 65535 512 }}512Bitwise AND
@band(12345, 678);{{ band 12345 678 }}32or band
@bitwiseAND(222, 111);{{ bitwiseAND 222 111 }}78or bitwiseAND
@@(1 | 2 | 4);{{ bor (bor 1 2) 4 }}7Bitwise OR
@bor(100, 200, 300);{{ bor 100 200 300 }}492or bor
@bitwiseOR(64, 256, 4);{{ bitwiseOR 64 256 4 }}324or bitwiseOR
@(1 ^ 2 ^ 4);{{ bxor (bxor 1 2) 4 }}7Bitwise XOR
@bxor(100, 200, 300);{{ bxor 100 200 300 }}384or bxor
@bitwiseXOR(64, 256, 4);{{ bitwiseXOR 64 256 4 }}324or bitwiseXOR
@(255 &^ 4);{{ bclear 255 4 }}-Bitwise Clear
@bclear(0xff, 3, 8);{{ bclear 0xff 3 8 }}-or bclear
@bitwiseClear(0xf, 7);{{ bitwiseClear 0xf 7 }}-or bitwiseClear

Other mathematic functions

Special cases

There are special behavior for certain operators depending of the arguments:

String multiplication

@("" * 100) will result in {{ mul “” 100 }} which result in: