問題描述
我寫了這段代碼:
float b = 3.6;
我明白了:
<上一頁>錯誤:未解決的編譯問題:類型不匹配:無法從 double 轉換為 float為什么?float
的定義是什么?
在 Java 中,當您鍵入一個十進制數為 3.6
時,它會被解釋為一個 double
.double
是 64 位精度的 IEEE 754 浮點,而 float
是 32 位精度的 IEEE 754 浮點.由于 float
不如 double
精確,因此無法隱式執行轉換.
如果你想創建一個浮點數,你應該以 f
結束你的數字(即:3.6f
).
更多解釋請參見Java教程的原始數據類型定義.
I wrote this code:
float b = 3.6;
and I get this:
Error:Unresolved compilation problem: Type mismatch: cannot convert from double to float
Why? Whats the definition of float
?
In Java, when you type a decimal number as 3.6
, its interpreted as a double
. double
is a 64-bit precision IEEE 754 floating point, while float
is a 32-bit precision IEEE 754 floating point. As a float
is less precise than a double
, the conversion cannot be performed implicitly.
If you want to create a float, you should end your number with f
(i.e.: 3.6f
).
For more explanation, see the primitive data types definition of the Java tutorial.
這篇關于Java中的浮點數是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!