The `truncate` method in Dart returns the integer obtained by discarding any fractional digits from the given number.
The syntax of double.truncate() method is:
int truncate()
This truncate() method of double returns the integer obtained by discarding any fractional digits from this
.
double.truncate() returns value of type int
.
In this example,
num
with the value 4.2.truncate()
method to truncate num
by discarding any fractional digits and obtain an integer.void main() {
double num = 4.2;
int truncated = num.truncate();
print('Truncated value of $num: $truncated');
}
Truncated value of 4.2: 4
In this example,
num
with the value 4.8.truncate()
method to truncate num
by discarding any fractional digits and obtain an integer.void main() {
double num = 4.8;
int truncated = num.truncate();
print('Truncated value of $num: $truncated');
}
Truncated value of 4.8: 4
In this example,
num
with the value -4.5.truncate()
method to truncate num
by discarding any fractional digits and obtain an integer.void main() {
double num = -4.5;
int truncated = num.truncate();
print('Truncated value of $num: $truncated');
}
Truncated value of -4.5: -4
In this Dart tutorial, we learned about truncate() method of double: the syntax and few working examples with output and detailed explanation for each example.