How to Round double with decimal length in Dart/Flutter

In this example, we are going to show you how to delete decimal point length with round and without a round in Dart or Flutter. See the example below:

double a = 34.5355323;
double b = double.parse(a.toStringAsFixed(3)); //3 is decimal length
print(b); //output: 34.536

double getNumber(double input, {int precision = 2}){
   return double.parse('$input'.substring(0, '$input'.indexOf('.') + precision + 1));
}

double a = 34.5355323;
double c = getNumber(a, precision: 3); //3 is decimal length
print(c); //output: 34.535

double a = 34.5355323;
double d = a.roundToDouble();
print(d);  //output: 35.0

double a = 34.5355323;
int  e = a.round(); //round gives int in return
print(e); //output: 35

double a = 34.5355323;
String s = a.toStringAsFixed(2);
print(s); //output: 34.54

In this way, you can round double in different ways in Dart or Flutter.

No any Comments on this Article


Please Wait...