【问题标题】:Dart: What is the difference between floor() and toInt()Dart: floor() 和 toInt() 有什么区别
【发布时间】:2021-12-17 08:01:44
【问题描述】:

我想截断 double 的所有小数而不进行四舍五入。我这里有两种可能:

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

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

【问题讨论】:

    标签: flutter dart truncate floor


    【解决方案1】:

    如文档所述:

    floor:

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

    toInt:

    相当于truncate

    truncate:

    将小数值向零舍入。

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

    【讨论】:

    • 看来,我心中只有积极的价值观。因此,在考虑负值时确实会有所不同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2021-02-12
    • 2020-08-16
    • 2014-01-21
    • 2019-08-19
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多