Java中的浮点数是什么?

What is float in Java?(Java中的浮点数是什么?)
本文介绍了Java中的浮点数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我写了这段代码:

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 floatis 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中的浮点数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)