The % (integer divide) operator divides two numbers and returns the integer part of the result. The result returned is defined to be that which would result from repeatedly subtracting the divisor from the dividend while the dividend is larger than the divisor.
The first way, use truncating division operator( ~/ ) operator to return the int type only. Second-way using return result type as var or double with division operator. In this example, use division operator to return of dynamic variable declared with var or double instead of int.
~/ Divide, returning an integer result. and ~/= integer division and assignment.
Division of integers involves the grouping of items. It includes both positive numbers and negative numbers. Just like multiplication, the division of integers also involves the same cases. When you divide integers with two positive signs, Positive ÷ Positive = Positive → 16 ÷ 8 = 2.
That is because Dart uses double
to represent all numbers in dart2js
. You can get interesting results, if you play with that:
Code:
int a = 1;
a is int;
a is double;
Result:
true
true
Actually, it is recommended to use type num
when it comes to numbers, unless you have strong reasons to make it int
(in for loop, for example). If you want to keep using int
, use truncating division like this:
int a = 500;
int b = 250;
int c;
c = a ~/ b;
Otherwise, I would recommend to utilize num
type.
Integer division is
c = a ~/ b;
you could also use
c = (a / b).floor();
c = (a / b).ceil();
if you want to define how fractions should be handled.
Short Answer
Use c = a ~/ b
.
Long Answer
According to the docs, int
are numbers without a decimal point, while double
are numbers with a decimal point.
Both double
and int
are subtypes of num
.
When two integers are divided using the /
operator, the result is evaluated into a double
. And the c
variable was initialized as an integer. There are at least two things you can do:
c = a ~/ b
.The ~/
operator returns an int
.
var c;
. This creates a dynamic variable that can be assigned to any type, including a double
and int
and String
etc. You can use the truncating division operator ~/
to get an integer result from a division operation:
4 ~/ 2; // 2 (int)
The regular division operator /
will always return a double
value at runtime (see the docs):
for (var i = 4; i == 4; i = 3) {
i / 2; // 2 (double)
}
You might have noticed that I wrote a loop for the second example (for the regular division operator) instead of 4 / 2
.
The reason for this is the following:
When an expression can be evaluated at compile time, it will be simplified at that stage and also be typed accordingly. The compiler would simply convert 4 / 2
to 2
at compile time, which is then obviously an int
. The loop prevents the compiler from evaluating the expression.
As long as your division happens at runtime (i.e. with variables that cannot be predicted at compile time), the return types of the /
(double
) and ~/
(int
) operators will be the types you will see for your expressions at runtime.
See this fun example for further reference.
Generally speaking, the regular division operator /
always returns a double
value and truncate divide can be used to get an int
result instead.
Compiler optimization might, however, cause some funky results :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With