How to round a number in dart?

For rounding doubles checkout: https://api.dartlang.org/stable/2.4.0/dart-core/double/round.html

Rounding won't work in your case because docs says:

  • Returns the integer closest to this.

So it will give 6 instead 5.57.

Your solution:

double x = 5.56753;
String roundedX = x.toStringAsFixed(2);
print(roundedX);

there is num class contained function round():

Num

 double numberToRound = 5.56753;
    print(numberToRound.round()); 
    //prints 6

If you want decimals

double n = num.parse(numberToRound.toStringAsFixed(2));
  print(n);
  //prints 5.57

check comment sujestion

Tags:

Dart