Dart: Working with numbers
This article is part of a series on How to start creating mobile applications for Android or iOS using Dart & Flutter.
Addition
When adding two numbers of the same type, the resulting type will be the same. When adding an integer type number to a double type number, the resulting type will be double.
int x = 3;
int y = 4;
var c = x + y; // Result is 7 and result type is int
x += y; // This is equal x = x + y;
double f = 2.3;
double g = 3.0;
var s = f + g; // Result is 5.3 and result type is double
int k = 2;
double t = 2.0;
var y = k + t; // Result is 4.0 and result type is doubleSubtraction
When subtracting two numbers of the same type, the resulting type will be the same. When subtracting where one number is of type int and the other number is of type double, the resulting type will be double.
int x = 3;
int y = 4;
var c = x - y; // Result is -1 and result type is int
x -= y; // This is equal x = x - y;
double f = 5.2;
double g = 3.0;
var s = f - g; // Result is 2.2 and result type is double
int k = 4;
double t = 2.0;
var y = k - t; // Result is 2.0 and result type is double
var h= t - k; // Result is -2.0 and result type is doubleMultiplication
When multiplying two numbers of the same type, the resulting type will be the same. When multiplying where one number is of type int and the other number is of type double, the resulting type will be double.
int x = 3;
int y = 4;
var c = x * y; // Result is 12 and result type is int
x *= y; // This is equal x = x * y;
double f = 5.2;
double g = 3.0;
var s = f * g; // Result is 15.6 and result type is double
int k = 4;
double t = 2.0;
var y = k * t; // Result is 8.0 and result type is doubleDivision
When performing a division operation, the resulting type will be double.
int x = 8;
int y = 2;
var c = x / y; // Result is 4 and result type is double
double g = 4.0;
int j = 2;
g /= j; // This is equal g = g / j;Integer division
The result of integer division will always be of type int.
double x = 11.4;
int y = 2;
var c = x ~/y; // Result is 5 and result type is int
int d = 5;
double s = 1.3;
d ~/= s; // This is equal d = d ~/ s; and result value is 3Remainder of integer division
Get Arte Bi’s stories in your inbox
Join Medium for free to get updates from this writer.
In this operation, if one of the numbers has the type double, the result will also have the type double.
int x = 11;
double y = 2.3;
var c = 11 % y; // Result is 1.8 and result type is double
double d = 11.0;
double f = 3.3;
d %= f; // This is equal d = d % f; and result value is 1.1Postfix increment/decrement
The operator is placed after the variable. It first returns the current value of the variable and then changes it.
int x = 10;
print(x++); // It will output 10, then increment x by 1
int y = 20;
print(y--); // It will output 20, then decrement y by 1
print(x); // Now x is equal to 11
print(y); // Now y is equal to 19Prefix increment/decrement
The operator is placed before the variable. It first changes the value of the variable and then returns the new value.
int a = 7;
print(++a); // It will increment a by 1 and output 8
int b = 12;
print(--b); // It will decrement b by 1 and output 11
print(a); // Now a is equal to 8
print(b); // Now b is equal to 11The order of arithmetic operations
The order of arithmetic operations in the Dart language is determined by the standard rules of mathematics. The operations are executed in the following order:
- The unary minus (-) is applied directly to a number, changing its sign.
- Multiplication (*) and division (/) are performed from left to right.
- Addition (+) and subtraction (-) are performed from left to right.
You can use parentheses “()” to explicitly specify the order of operations if you want to change the standard order.
int result1 = 5 + 3 * 2; // First multiplication, then addition: 11
int result2 = (5 + 3) * 2; // First parentheses, then multiplication: 16
double result3 = 10 / 2 + 3; // First division, then addition: 8.0
double result4 = 10 / (2 + 3); // First parentheses, then division: 2.0
int result5 = -5 + 8 * 2; // Unary minus, then multiplication and addition: 11If there are multiple operations of the same type in an expression (for example, multiple additions or multiplications), they are performed from left to right, regardless of their arrangement.
int result6 = 10 - 2 - 3; // Subtraction is performed from left to right: 5
int result7 = 2 * 3 * 4; // Multiplication is performed from left to right: 24These rules allow you to control the order of arithmetic operations in Dart and ensure accurate calculation results. If you need to modify the order of operations, use parentheses to explicitly specify the priority.









