In this post, we will be practically discussing 4 ways through which we can convert Dart double to int with ease. We will be using multiple Dart code examples for demonstration. So let’s get right into discussing these multiple ways used to convert Dart double to int.
What is Dart Double to Int?
It defines the process of converting Dart double(decimal) values to integers. Let’s start understanding it with proper Dart examples.
Implementing Dart Double To Int (4 Ways)
Listed below are the 4 ways that are used to convert Dart double to int:
- toInt()
- floor()
- round()
- ceil()
Using toInt()
Using this method, we can easily convert Dart double to int. See the below example for practical demonstration:
double value=34.56; var newValue =value.toInt(); print(newValue); print(newValue.runtimeType);
Output:
34 int
Using floor()
It converts the double value to the largest integer value that is lesser than the original double value. See below code example:
double value=345.98; var newValue =value.floor(); print(newValue); print(newValue.runtimeType);
Output:
345 int
Using round()
It stores the value of the closed integer to the original double value. If it is 4.4 then it will show 4, if it is 4.6 then it will show 5 and even if it is 4.5, then still it will show 5. See below examples for practical demonstration:
Example 1
double value=3.5; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
4 int
Example 2
double value=3.4; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
3 int
Example 3
double value=3.6; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
4 int
Using ceil()
It is used to show the integer that is greater than the the double value but the closest integer value to that double. See below examples:
Example 1
double value=3.1; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
4 int
Example 2
double value=3.01; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
4 int
Example 3
double value=3.8; var newValue =value.round(); print(newValue); print(newValue.runtimeType);
Output:
4 int
So this is how you can easily change Dart double to int using multiple ways. Hope you like this post. Don’t hesitate to ask if you still have questions related to the conversion of Dart double to int. I would be very happy to answer all.
Conclusion
In conclusion, we have used 4 ways through which we can easily convert Dart double to int. I would love to have your thoughts on this post. Thank you for reading this post.