Dart double operator-truncating-division
Syntax & Examples
double.operator-truncating-division operator
The `~/` operator is used for truncating division, which divides one number by another and returns the integer result by discarding any fractional digits.
Syntax of double.operator-truncating-division
The syntax of double.operator-truncating-division operator is:
operator ~/(num other) → intThis operator-truncating-division operator of double truncating division operator.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | the number by which this number will be divided |
✐ Examples
1 Truncate divide two numbers (10.0 ~/ 3.0)
In this example,
- We use the `~/` operator to perform truncating division of
10.0by3.0. - The result is stored in the variable
result. - We print the result to standard output.
Dart Program
void main() {
double result = 10.0 ~/ 3.0;
print('Result: $result');
}Output
Result: 3
2 Truncate divide two variables (a ~/ b)
In this example,
- We define two variables
aandbwith values15.0and4.0respectively. - We then use the `~/` operator to perform truncating division of
abyb. - The result is stored in the variable
result. - We print the result to standard output.
Dart Program
void main() {
double a = 15.0;
double b = 4.0;
int result = a ~/ b;
print('Result: $result');
}Output
Result: 3
3 Truncate divide a negative number by a positive number (-20.0 ~/ 4.0)
In this example,
- We use the `~/` operator to perform truncating division of
-20.0by4.0. - The result is stored in the variable
result. - We print the result to standard output.
Dart Program
void main() {
double result = -20.0 ~/ 4.0;
print('Result: $result');
}Output
Result: -5
Summary
In this Dart tutorial, we learned about operator-truncating-division operator of double: the syntax and few working examples with output and detailed explanation for each example.