簡體   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