Dart num truncateToDouble()
Syntax & Examples
num.truncateToDouble() method
The `truncateToDouble` method in Dart returns the double integer value obtained by discarding any fractional digits from the double value of this number.
Syntax of num.truncateToDouble()
The syntax of num.truncateToDouble() method is:
double truncateToDouble()
This truncateToDouble() method of num returns the double integer value obtained by discarding any fractional digits from the double value of this.
Return Type
num.truncateToDouble() returns value of type double
.
✐ Examples
1 Truncating a decimal number
In this example,
- We create a num variable
num1
with the value 4.8. - We use the
truncateToDouble()
method to truncate it to the nearest double integer value. - We then print the result to standard output.
Dart Program
void main() {
num num1 = 4.8;
double truncatedValue = num1.truncateToDouble();
print('Truncated value of $num1: $truncatedValue');
}
Output
Truncated value of 4.8: 4
2 Truncating another decimal number
In this example,
- We create a num variable
num1
with the value -3.2. - We use the
truncateToDouble()
method to truncate it to the nearest double integer value. - We then print the result to standard output.
Dart Program
void main() {
num num1 = -3.2;
double truncatedValue = num1.truncateToDouble();
print('Truncated value of $num1: $truncatedValue');
}
Output
Truncated value of -3.2: -3
3 Truncating an integer
In this example,
- We create a num variable
num1
with the value 7.0, which is already an integer. - We use the
truncateToDouble()
method to truncate it to the nearest double integer value. - We then print the result to standard output.
Dart Program
void main() {
num num1 = 7.0;
double truncatedValue = num1.truncateToDouble();
print('Truncated value of $num1: $truncatedValue');
}
Output
Truncated value of 7: 7
Summary
In this Dart tutorial, we learned about truncateToDouble() method of num: the syntax and few working examples with output and detailed explanation for each example.