除了 Alexandre Ardhuin 的回答:
据我所知,dart 在投射方面不是那么灵活,因此不建议使用(在这种情况下甚至不允许使用)。
您可以使用 round() 函数:
int count = 1;
int size = 3;
var percent = (count * 100 / size);
print(percent);
int asInt = percent.round();
print(asInt);
或者如果你想要典型的interger rounding,请使用floor():
int count = 1;
int size = 3;
var percent = (count * 100 / size);
print(percent);
int asInt = percent.floor();
print(asInt);
注意: 在这些示例中,percent 是一个双精度值,可以存储起来供以后使用。
为了完整起见,包括
ceil:
int count = 2;
int size = 3;
var percent = (count * 100 / size);
print(percent);
int asIntRound = percent.round();
print(asIntRound);
int asIntFloor = percent.floor();
print(asIntFloor);
int asIntCeil = percent.ceil();
print(asIntCeil);
输出:
66.66666666666667
67
66
67