Dart double Data Type by Examples
In this tutorial, we learn how to use double-precision floating point number data type in Dart programming language.
How to declare double variables and basic operators in Dart
void main() {
double number1 = 10.5;
double number2 = 5.5;
double addResult = number1 + number2;
double subtractResult = number1 - number2;
double multuplyResult = number1 * number2;
double divisionResult = number1 / number2;
double moduloResult = number1 % number2;
print("10.5 + 5.5 = " + addResult.toString());
print("10.5 - 5.5 = " + subtractResult.toString());
print("10.5 * 5.5 = " + multuplyResult.toString());
print("10.5 / 5.5 = " + divisionResult.toString());
print("10.5 % 5.5 = " + moduloResult.toString());
}
10.5 + 5.5 = 16.0
10.5 - 5.5 = 5.0
10.5 * 5.5 = 57.75
10.5 / 5.5 = 1.9090909090909092
10.5 % 5.5 = 5.0
Check a double value is negative or not using isNegative property
void main() {
double number1 = -10.0;
double number2 = -0.0;
double number3 = 0.0;
double number4 = 10.0;
print(number1.isNegative);
print(number2.isNegative);
print(number3.isNegative);
print(number4.isNegative);
}
true
true
false
false
Get absolute value of a double variable using abs() method
void main() {
double number1 = -5;
double number2 = 5;
print(number1.abs());
print(number2.abs());
}
5.0
5.0
How to use ceil() method
Dart’s double class provides ceil() method to return the least integer no smaller than a given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.ceil());
print(number2.ceil());
}
6
6
How to use ceilToDouble() method
Dart’s double class provides ceilToDouble() method to return the least integer double value no smaller than a given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.ceilToDouble());
print(number2.ceilToDouble());
}
6.0
6.0
How to use floor() method
Dart’s double class provides floor() method to return the greatest integer no greater than a given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.floor());
print(number2.floor());
}
5
5
How to use floorToDouble() method
Dart’s double class provides floorToDouble() method to return the greatest integer double value no greater than a given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.floorToDouble());
print(number2.floorToDouble());
}
5.0
5.0
How to use round() method
Dart’s double class provides round() method to return the integer value closest to the given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.round());
print(number2.round());
}
6
5
How to use roundToDouble() method
Dart’s double class provides roundToDouble() method to return the integer double value closest to the given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.roundToDouble());
print(number2.roundToDouble());
}
6.0
5.0
How to use truncate() method
Dart’s double class provides truncate() method to return the integer obtained by discarding any fractional digits from the given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.truncate());
print(number2.truncate());
}
5
5
How to use truncateToDouble() method
Dart’s double class provides truncateToDouble() method to return the double integer obtained by discarding any fractional digits from the given double value.
void main() {
double number1 = 5.5;
double number2 = 5.1;
print(number1.truncateToDouble());
print(number2.truncateToDouble());
}
5.0
5.0
How to use compareTo() method
The double’s compareTo() method returns a negative number if the value is less than the compare to value, zero if it equals and return positive number if it is greater than the compare to value.
void main() {
double number1 = 5.5;
double number2 = 10.5;
print(number1.compareTo(number2));
print(number1.compareTo(5.5));
print(number1.compareTo(1.0));
print((-0.0).compareTo(0.0));
}
-1
0
1
-1
How to use remainder() method
The double’s remainder() method to return the remainder of the truncating division of 2 double values.
void main() {
double number1 = 10;
double number2 = 3;
print(number1.remainder(number2));
}
1.0
Convert double value to integer value in Dart
The double’s toInt() method to truncate the double value and return as an integer value.
void main() {
double number = 10.5;
print(number.toInt());
}
10
Convert double value to String value in Dart
Using toString() method
void main() {
double number = 200.123;
print(number.toString());
}
200.123
Using toStringAsExponential() method
void main() {
double number = 200.123;
print(number.toStringAsExponential(1));
print(number.toStringAsExponential(2));
print(number.toStringAsExponential(3));
print(number.toStringAsExponential(4));
}
2.0e+2
2.00e+2
2.001e+2
2.0012e+2
Using toStringAsFixed() method
void main() {
double number = 200.123;
print(number.toStringAsFixed(1));
print(number.toStringAsFixed(2));
print(number.toStringAsFixed(3));
print(number.toStringAsFixed(4));
}
200.1
200.12
200.123
200.1230
Parse a String to a double value
Using parse() static method
void main() {
double number1 = double.parse("10.5");
double number2 = double.parse("-10.5");
double number3 = double.parse(" 5.");
double number4 = double.parse(" .5 ");
double number5 = double.parse("-1.e5");
double number6 = double.parse("1234E+7");
double number7 = double.parse("+.12e-9");
double number8 = double.parse("-NaN");
print(number1);
print(number2);
print(number3);
print(number4);
print(number5);
print(number6);
print(number7);
print(number8);
}
10.5
-10.5
5.0
0.5
-100000.0
12340000000.0
1.2e-10
NaN
Using tryParse() static method
void main() {
double number1 = double.tryParse("10.5");
double number2 = double.tryParse("-10.5");
double number3 = double.tryParse(" 5.");
double number4 = double.tryParse(" .5 ");
double number5 = double.tryParse("-1.e5");
double number6 = double.tryParse("1234E+7");
double number7 = double.tryParse("+.12e-9");
double number8 = double.tryParse("-NaN");
print(number1);
print(number2);
print(number3);
print(number4);
print(number5);
print(number6);
print(number7);
print(number8);
}
10.5
-10.5
5.0
0.5
-100000.0
12340000000.0
1.2e-10
NaN
The double’s constants
void main() {
print(double.minPositive);
print(double.maxFinite);
print(double.infinity);
print(double.negativeInfinity);
print(double.nan);
}
5e-324
1.7976931348623157e+308
Infinity
-Infinity
NaN
Happy Coding 😊
Related Articles
Dart integer Data Type by Examples
Dart bool Data Type by Examples
Dart String Data Type by Examples