Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Math.round() in Dart?

Tags:

dart

People also ask

How do you round up numbers in flutter?

To round up to the nearest whole number in an absolute sense, use ceil if the number is positive and floor if the number is smaller than 0. The following function rounds numbers up to the closest integer. Or, use an extension function ( 6.3. roundUpAbs ).

What does ~/ mean in DART?

~/ Divide, returning an integer result. and ~/= integer division and assignment.


Yes, there is a way to do this. The num class has a method called round():

var foo = 6.28;
print(foo.round()); // 6

var bar = -6.5;
print(bar.round()); // -7

In Dart, everything is an object. So, when you declare a num, for example, you can round it through the round method from the num class, the following code would print 6

num foo = 5.6;
print(foo.round()); //prints 6

In your case, you could do:

main() {
    print((5.5).round());
}

This equation will help you

int a = 500;
int b = 250;
int c;

c = a ~/ b;

UPDATE March 2021:

The round() method has moved to https://api.dart.dev/stable/2.12.2/dart-core/num/round.html. All the above links are wrong.


Maybe this can help in specific situations, floor() will round towards the negative infinite

https://api.dart.dev/stable/2.13.4/dart-core/num/floor.html

void main() {
 var foo = 3.9;
 var bar = foo.floor();
 print(bar);//prints 3
}