Numeric Operators
The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and remainder (%), as shown in Table 2.3. When both operands of a division are integers, the result of the division is an integer. The fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields -2, not –2.5. To perform regular mathematical division, one of the operands must be a floating-point number. For example, 5.0 / 2 yields 2.5. The % operator yields the remainder after division. The left-hand operand is the dividend and the right-hand operand the divisor. Therefore, 7 % 3 yields 1, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7.
Name Meaning Example Result
+ Addition 34 + 1 35
- Subtraction 34.0–0.1 33.9
* Multiplication 300 * 30 9000
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2