Dart BigInt compareTo()
Syntax & Examples
BigInt.compareTo() method
The `compareTo()` method compares this BigInt to another BigInt.
Syntax of BigInt.compareTo()
The syntax of BigInt.compareTo() method is:
int compareTo(BigInt other)
This compareTo() method of BigInt compares this to other
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
other | required | the BigInt to compare to |
Return Type
BigInt.compareTo() returns value of type int
.
✐ Examples
1 Compare two positive BigInts
In this example,
- We create two BigInts,
bigInt1
with a value of10
andbigInt2
with a value of5
. - We use the
compareTo()
method to comparebigInt1
withbigInt2
. - We print the comparison result to standard output.
Dart Program
void main() {
BigInt bigInt1 = BigInt.from(10);
BigInt bigInt2 = BigInt.from(5);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Output
Comparison result: 5
2 Compare two equal BigInts
In this example,
- We create two equal BigInts,
bigInt1
andbigInt2
, both with a value of100
. - We use the
compareTo()
method to comparebigInt1
withbigInt2
. - We print the comparison result to standard output.
Dart Program
void main() {
BigInt bigInt1 = BigInt.from(100);
BigInt bigInt2 = BigInt.from(100);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Output
Comparison result: 0
3 Compare two negative BigInts
In this example,
- We create two BigInts,
bigInt1
with a value of-5
andbigInt2
with a value of-10
. - We use the
compareTo()
method to comparebigInt1
withbigInt2
. - We print the comparison result to standard output.
Dart Program
void main() {
BigInt bigInt1 = BigInt.from(-5);
BigInt bigInt2 = BigInt.from(-10);
int comparison = bigInt1.compareTo(bigInt2);
print('Comparison result: $comparison');
}
Output
Comparison result: 5
Summary
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.