如何在 Dart 中将 double 转换为 int ?

Kum*_*ong 2 dart flutter

我有这样的代码

double total = 20.2;
int min = total as int;
Run Code Online (Sandbox Code Playgroud)

flutter 构建没问题,但运行时出现异常。

我尝试过 int.parse() 但它需要字符串而不是双精度。

您有什么建议吗?

小智 5

如果您想对值进行四舍五入:

double total = 20.2;
int min = total.round();
Run Code Online (Sandbox Code Playgroud)

如果您只想截断该值:

double total = 20.2;
int min = total.toInt();
Run Code Online (Sandbox Code Playgroud)