Dart double truncateToDouble()
Syntax & Examples

double.truncateToDouble() method

The `truncateToDouble` method returns the integer double value obtained by discarding any fractional digits from the given double.


Syntax of double.truncateToDouble()

The syntax of double.truncateToDouble() method is:

 double truncateToDouble() 

This truncateToDouble() method of double returns the integer double value obtained by discarding any fractional digits from this.

Return Type

double.truncateToDouble() returns value of type double.



✐ Examples

1 Truncate a positive double value

In this example,

  1. We define a double num with a value of 3.14.
  2. We call the truncateToDouble() method on num to obtain its truncated value.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = 3.14;
  double truncated = num.truncateToDouble();
  print('Truncated value of 3.14: $truncated');
}

Output

Truncated value of 3.14: 3

2 Truncate a negative double value

In this example,

  1. We define a double num with a value of -5.6.
  2. We call the truncateToDouble() method on num to obtain its truncated value.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = -5.6;
  double truncated = num.truncateToDouble();
  print('Truncated value of -5.6: $truncated');
}

Output

Truncated value of -5.6: -5

3 Truncate a double value with fractional digits

In this example,

  1. We define a double num with a value of 10.9.
  2. We call the truncateToDouble() method on num to obtain its truncated value.
  3. We print the result to standard output.

Dart Program

void main() {
  double num = 10.9;
  double truncated = num.truncateToDouble();
  print('Truncated value of 10.9: $truncated');
}

Output

Truncated value of 10.9: 10

Summary

In this Dart tutorial, we learned about truncateToDouble() method of double: the syntax and few working examples with output and detailed explanation for each example.