将一个浮点数的内容转移到Java中的长整型变量中

huangapple go评论61阅读模式
英文:

Moving the contents of a float to a long in Java

问题

以下是翻译好的内容:

这个问题在网站上的示例是:

“将一个声明为浮点数据类型的变量的内容移动到一个声明为长整型数据类型的变量中,需要使用_____。”

答案:一个类型转换

解释:需要显式类型转换“float”

long wayHome = 123456789;

float myBoat = (float) wayHome;

但是在我看来,这似乎是在相反的方向上进行操作 —— 将一个长整型变量转移到一个浮点型变量中,而不是相反的操作。

我是错的,还是问题本身有问题?是隐式转换还是类型转换?

英文:

The example given for this problem on the site is:

"To move the contents of a variable declared as data type float into a variable declared as data type long requires the use of _____."

Answer: a type cast

Explanation: The explicit type cast "float" is required

long wayHome = 123456789;

float myBoat = (float) wayHome;

But this appears to be doing the opposite to me -- moving a long into a float, not the other way around.

Am I wrong, or is the question wrong? Would it be implicit or typecast?

答案1

得分: 3

给定的示例确实与问题文本中描述的相反,就像你所假设的那样。
然而,将一个 float 值分配给一个 long 变量也需要显式转换,例如:

float myBoat = 123.456;

long wayHome = (long) myBoat;
英文:

The example given is indeed the opposite of what's described in the question's text, like you assumed.
However, assigning a float value to a long variable will also require an explicit cast, e.g.:

float myBoat = 123.456

long wayHome = (long) myBoat;

huangapple
  • 本文由 发表于 2020年4月10日 06:22:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61131174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定