The `compareTo()` method compares this BigInt to another BigInt.
The syntax of BigInt.compareTo() method is:
int compareTo(BigInt other)
This compareTo() method of BigInt compares this to other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the BigInt to compare to |
BigInt.compareTo() returns value of type int
.
In this example,
bigInt1
with a value of 10
and bigInt2
with a value of 5
.compareTo()
method to compare bigInt1
with bigInt2
.void main() {
BigInt bigInt1 = BigInt.from(10);
BigInt bigInt2 = BigInt.from(5);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Comparison result: 5
In this example,
bigInt1
and bigInt2
, both with a value of 100
.compareTo()
method to compare bigInt1
with bigInt2
.void main() {
BigInt bigInt1 = BigInt.from(100);
BigInt bigInt2 = BigInt.from(100);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Comparison result: 0
In this example,
bigInt1
with a value of -5
and bigInt2
with a value of -10
.compareTo()
method to compare bigInt1
with bigInt2
.void main() {
BigInt bigInt1 = BigInt.from(-5);
BigInt bigInt2 = BigInt.from(-10);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Comparison result: 5
In this Dart tutorial, we learned about compareTo() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.