Dart num truncate()
Syntax & Examples
num.truncate() method
The `truncate` method in Dart returns the integer obtained by discarding any fractional digits from this number.
Syntax of num.truncate()
The syntax of num.truncate() method is:
int truncate()
This truncate() method of num returns the integer obtained by discarding any fractional digits from this.
Return Type
num.truncate() returns value of type int
.
✐ Examples
1 Truncating a decimal number
In this example,
- We create a num variable
number1
with the value 5.6 and another variablenumber2
with the value -3.2. - We use the
truncate()
method to obtain their truncated integer values. - We then print the truncated values to standard output.
Dart Program
void main() {
num number1 = 5.6;
num number2 = -3.2;
int result1 = number1.truncate();
int result2 = number2.truncate();
print('Truncated value of $number1: $result1');
print('Truncated value of $number2: $result2');
}
Output
Truncated value of 5.6: 5 Truncated value of -3.2: -3
2 Truncating a decimal number with fractional part
In this example,
- We create a num variable
number
with the value 10.9. - We use the
truncate()
method to obtain its truncated integer value. - We then print the truncated value to standard output.
Dart Program
void main() {
num number = 10.9;
int result = number.truncate();
print('Truncated value of $number: $result');
}
Output
Truncated value of 10.9: 10
Summary
In this Dart tutorial, we learned about truncate() method of num: the syntax and few working examples with output and detailed explanation for each example.