Home > Mobile >  How to convert / round a double value to int or String in flutter dart
How to convert / round a double value to int or String in flutter dart

Time:06-25

here is a double value

final dblValue = 25.5;

and I want this number as this int or string. I tried the replaceAll function to remove the value after the ., but that didn't work too, I just need the number only without. value. how do I do that in dart

CodePudding user response:

You can use the .floor() method which goes to the int value without the decimal points. For a formal definition:

The greatest integer no greater than this number.

final d = 25.9;
final d2 = 25.1;


int i = d.floor(); // i = 25
int i2 = d2.floor(); // i2 = 25

If you then want it as a string you can use the .toString() method on the int you just did.

CodePudding user response:

To convert it to a string use

dblValue.toString();

To round it use

dblValue.round();

Or

dblValue.toInt(); 

And to floor the value use

dblValue.floor();

CodePudding user response:

use the .floor() method

final dblValue = 25.5;
int value = d.floor(); // value = 25
  • Related