繁体   English   中英

Dart: floor() 和 toInt() 有什么区别

[英]Dart: What is the difference between floor() and toInt()

我想在不四舍五入的情况下截断 double 的所有小数。 我这里有两种可能:

double x = 13.5;
int x1 = x.toInt(); // x1 = 13
int x2 = x.floor(); // x2 = 13

这两种方法有什么区别吗?

正如文档所解释的:

floor :

将小数值向负无穷大舍入。

toInt :

相当于truncate

truncate

将小数值向零舍入。

所以floor向负无穷大toInt ,但toInt / truncate向零舍入。 对于正值,这无关紧要,但对于负小数值, floor将返回一个小于原始值的数字,而toInt / truncate将返回一个更大的数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM