How To Easily Convert Dart Double to Int (4 Easy Ways)

dart double to int

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:

  1. toInt()
  2. floor()
  3. round()
  4. 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
You can see that first we initialize a double variable. We then declare and value with var and pass it value.toInt() which will convert that double to int. You can see that both the value and type is now integer.

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
You can see that the largest integer value is shown but still smaller than the double value.

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.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *