Where is Math.round() in Dart?

2020-06-30 04:27发布

问题:

I don't see any way to round a number in Dart?

import 'dart:math';

main() {
  print(Math.round(5.5)); // Error!
}

http://api.dartlang.org/docs/bleeding_edge/dart_math.html

回答1:

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


回答2:

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());
}


回答3:

This equation will help you

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

c = a ~/ b;


回答4:

UPDATE June 2020:

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



标签: dart