Dart integer Data Type by Examples
In this tutorial, we learn how to use int data type in a Dart program.
How to define int variable and basic operator in Dart
void main() {
int number1 = 10;
int number2 = 20;
int addResult = number1 + number2;
int subtractResult = number2 - number1;
int multiplyResult = number1 * number2;
double divisionResult = number2 / number1;
int moduloResult = number2 % number1;
print("10 * 20 = " + addResult.toString());
print("20 -10 = " + subtractResult.toString());
print("10 * 20 = " + multiplyResult.toString());
print("20 / 10 = " + divisionResult.toString());
print("20 % 10 = " + moduloResult.toString());
}
10 * 20 = 30
20 -10 = 10
10 * 20 = 200
20 / 10 = 2.0
20 % 10 = 0
Get number of bits using bitLength property
The int class provides bitLength property to return the minimum number of bits needed to store the value in memory.
void main() {
int number1 = 100;
print(number1.bitLength);
int number2 = 1234567890;
print(number2.bitLength);
int number3 = 0;
print(number3.bitLength);
int number4 = -1;
print(number4.bitLength);
int number5 = -1000;
print(number5.bitLength);
}
7
31
0
0
10
Check an integer number is even or not using isEven property
void main() {
int number1 = 0;
print(number1.isEven);
int number2 = 1;
print(number2.isEven);
int number3 = 2;
print(number3.isEven);
}
true
false
true
Check an integer number is odd or not using isOdd property
void main() {
int number1 = 0;
print(number1.isOdd);
int number2 = 1;
print(number2.isOdd);
int number3 = 2;
print(number3.isOdd);
}
false
true
false
Get sign of integer value
void main() {
int number1 = -100;
print(number1.sign);
int number2 = 0;
print(number2.sign);
int number3 = 100;
print(number3.sign);
}
-1
0
1
Get absolute value of integer value
void main() {
int number1 = -100;
print(number1.abs());
int number2 = 0;
print(number2.abs());
int number3 = 100;
print(number3.abs());
}
100
0
100
Compare two integer values
Dart provides the int.compareTo() method which returns negative if the integer value less than value compared to, zero if it equals and positive value if it greater than value compared to.
void main() {
int number1 = 10;
int number2 = 20;
int number3 = 5;
print(number1.compareTo(number2));
print(number1.compareTo(number3));
print(number1.compareTo(10));
}
-1
1
0
Get greatest common divisor value of two integer values
void main() {
int number1 = 2;
int number2 = 4;
print(number1.gcd(number2));
print(1.gcd(5));
}
2
1
Convert integer to double value using toDouble method
void main() {
int number1 = -2;
int number2 = 0;
int number3 = 2;
print(number1.toDouble());
print(number2.toDouble());
print(number3.toDouble());
}
-2.0
0.0
2.0
Convert integer to String value using toString method
void main() {
int number1 = -2;
int number2 = 0;
int number3 = 2;
print(number1.toString());
print(number2.toString());
print(number3.toString());
}
-2
0
2
Happy Coding 😊
Related Articles
Dart double Data Type by Examples
Dart bool Data Type by Examples
Implement Hello World Program in Dart Programming Language
Dart String Data Type by Examples