Operators in JQuery Calculate widget

18 Apr 20171 minute to read

The calculation type of an equation will be specified by operators. These operators will be prioritized in a default order for calculating the equation.

To recognize a particular string is to be treated as a formula, the string is required to start with a special character formulaCharacter. Its default character is the equals = sign.

Arithmetic operators

The mathematical operations such as addition, subtraction, multiplication etc can be performed using the arithmetic operators.

Arithmetic operator denotation Example
+ (plus sign) Addition 1 + 2
- (minus sign - unary) Negation - 5
- (minus sign - binary) Subtraction 4 - 2
* (asterisk) Multiplication 2 * 5
/ (forward slash) Division 10 / 2
^ (caret) Exponentiation 5 ^ 2

Logical Operators

The logical or comparison operators will be used to compare two values of the formula. The return value will be either True or False.

Logical operator denotation Example
< (less than sign) Less than 5 < A2
> (greater than sign) Greater than A1 > A2
= (Equal sign) Equal to A1 = 4
<= (Less than or equal) Less than or equal A3 <= (A4 + 2)
>= (Greater than or equal) Greater than or equal B1 >= 30
<>(Not equal) Not equal C2 <>10

Text concatenation operator (Binary literal operator)

Two or more text strings can be concatenated into one text using this operator.

Logical operator denotation Example
& (ampersand) Concatenation “Hello” & “World”

Operator precedence

All operations are subjected to the following hierarchy of operations. The level 1 operations are done first, followed by level 2, and so on. Within the same level, the operations are performed from left to right in the order where they are encountered during the parsing of the formula.

  1. (Unary Minus)

  2. /

    • -
  3. < >= <= >= <>

  4. & (Concatenation)

When the default operators’ precedence need to be changed, then the parentheses will be used to explicitly indicate the operation order.

Examples

Formulas Computed Value
= 6 / 2 + 1 4
= 6 / (2 + 1) 2
= 2 + 4 / 2 4
= (2 + 4) / 2 3