The `compareTo` method compares the given number to another number.
The syntax of double.compareTo() method is:
int compareTo(num other)
This compareTo() method of double compares this to other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the number to compare with |
double.compareTo() returns value of type int
.
In this example,
num1
with a value of 5.5
and num2
with a value of 7.7
.compareTo
method on num1
with num2
as the argument.void main() {
double num1 = 5.5;
double num2 = 7.7;
int result = num1.compareTo(num2);
print('Comparison result: $result');
}
Comparison result: -1
In this example,
num1
with a value of -3.3
and num2
with a value of -1.1
.compareTo
method on num1
with num2
as the argument.void main() {
double num1 = -3.3;
double num2 = -1.1;
int result = num1.compareTo(num2);
print('Comparison result: $result');
}
Comparison result: -1
In this example,
num1
and num2
, both with a value of 10.0
.compareTo
method on num1
with num2
as the argument.void main() {
double num1 = 10.0;
double num2 = 10.0;
int result = num1.compareTo(num2);
print('Comparison result: $result');
}
Comparison result: 0
In this Dart tutorial, we learned about compareTo() method of double: the syntax and few working examples with output and detailed explanation for each example.