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) → int

This operator-truncating-division operator of double truncating division operator.

Parameters

ParameterOptional/RequiredDescription
otherrequiredthe number by which this number will be divided


✐ Examples

1 Truncate divide two numbers (10.0 ~/ 3.0)

In this example,

  1. We use the `~/` operator to perform truncating division of 10.0 by 3.0.
  2. The result is stored in the variable result.
  3. 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,

  1. We define two variables a and b with values 15.0 and 4.0 respectively.
  2. We then use the `~/` operator to perform truncating division of a by b.
  3. The result is stored in the variable result.
  4. 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,

  1. We use the `~/` operator to perform truncating division of -20.0 by 4.0.
  2. The result is stored in the variable result.
  3. 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.