Java,不可能将 Object 转换为 Float...为什么?

发布于 2024-11-07 08:08:07 字数 372 浏览 0 评论 0原文

为什么这有效:

 Object prova = 9.2;
 System.out.println(prova);
 Double prova2 = (Double) prova;
 System.out.println(prova2);

但这不起作用?

Object prova = 9.2;
System.out.println(prova);
Float prova2 = (Float) prova;
System.out.println(prova2);

我在我的java android应用程序中损失了1个小时,所以我不得不将它转换为双精度,然后将双精度转换为浮点数,否则我有一个异常

Why this works:

 Object prova = 9.2;
 System.out.println(prova);
 Double prova2 = (Double) prova;
 System.out.println(prova2);

And this doesn't?

Object prova = 9.2;
System.out.println(prova);
Float prova2 = (Float) prova;
System.out.println(prova2);

I lost 1 hour in my java android application cause of this thing so i had to cast it in a double and than the double in a float or i had an exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

演出会有结束 2024-11-14 08:08:07

因为你在编写时依赖于自动装箱

Object prova = 9.2;

如果你希望它是一个 Float,请尝试

Object prova = 9.2f;

记住 java.lang.Float 和 java.lang.Double 是同级类型;常见类型是 java.lang.Number

如果您想以任何格式表达数字,请使用 API,例如 Number.floatValue()

Because you are relying on autoboxing when you wrote

Object prova = 9.2;

If you want it to be a Float, try

Object prova = 9.2f;

Remember that java.lang.Float and java.lang.Double are sibling types; the common type is java.lang.Number

If you want to express a Number in whatever format, use the APIs, for example Number.floatValue()

念三年u 2024-11-14 08:08:07

因为 provaDouble,而 Double 不是 Float 的子类型。

您可以从浮点文字开始:9.2f(在这种情况下,prova实际上是一个Float),或者您可以像这样:

Float prova2 = ((Double) prova).floatValue();

Because prova is a Double, and Double is not a subtype of Float.

Either you could start with a float literal: 9.2f (in which case prova would actually be a Float) or, you could it like this:

Float prova2 = ((Double) prova).floatValue();
羞稚 2024-11-14 08:08:07

9.2 是一个双重文字。请尝试使用 9.2f

Object prova = 9.2f; // float literal is auto-boxed to a Float
System.out.println(prova);
Float prova2 = (Float) prova; // Float can be cast to Float, while Double cannot
System.out.println(prova2);

错误消息(您可能应该将其包含在您的问题中)也很好地解释了它:

Exception in thread "main" java.lang.ClassCastException: 
    java.lang.Double cannot be cast to java.lang.Float

9.2 is a double literal. Try 9.2f instead.

Object prova = 9.2f; // float literal is auto-boxed to a Float
System.out.println(prova);
Float prova2 = (Float) prova; // Float can be cast to Float, while Double cannot
System.out.println(prova2);

The error message (which you probably should have included in your question) explains it quite well also:

Exception in thread "main" java.lang.ClassCastException: 
    java.lang.Double cannot be cast to java.lang.Float
香草可樂 2024-11-14 08:08:07

因为如果你不指定,它将是一个双精度。如果你希望它是一个浮动,你需要

Object prova = 9.2F;
System.out.println(prova);
Float prova2 = (Float) prova;
System.out.println(prova2);

Because if you don't specify, it will be a double. If you want it to be a float, you need

Object prova = 9.2F;
System.out.println(prova);
Float prova2 = (Float) prova;
System.out.println(prova2);
勿挽旧人 2024-11-14 08:08:07

尝试将其转换为 Float 而不是 float
简单的

Try Casting it as Float not float
Simple

青衫儰鉨ミ守葔 2024-11-14 08:08:07

嗨,我也遇到了这个问题,我花了很长时间才想出这个非常简单的解决方案。
对于遇到此问题的任何其他人,并且您可能不想或无权更改您正在接收的对象,转换对象值的另一种方法是首先将 Double 值转换为 double 变量类型。然后简单地将 double 转换为 float... 示例:(

我还没有测试过这个,但根据最初发布的问题做了这个)

Object prova = 9.2;
System.out.println(prova);
double dblValProva = prova;
float pova2 = (float) dblValProva ;
System.out.println(prova2);

(这是我自己使用的一个示例,希望这些示例将来可以帮助其他人)

JSONArray color // This is a JSON array containing 3 values... 
//for instance [ 0.501, 0.501, 0.0 ]
double r = (Double) color.get(0);
double g = (Double) color.get(1);
double b = (Double) color.get(2);
Color3f floorColor = new Color3f((float)r, (float)g, (float)b);

对我来说以前当我尝试使用时出现错误:

Color3f((float)color.get(0), (float)color.get(1), (float)color.get(2))

Hi I hit this problem too and it took me a very long time to come up with this very simple solution.
For anyone else who hits this problem and you may not want to or have access to changing the object you are receiving, an alternative to converting object values is to first convert your Double value to a double variable type. Then simply cast double to float... Examples:

(I have not tested this but made this based on the initial posted problem)

Object prova = 9.2;
System.out.println(prova);
double dblValProva = prova;
float pova2 = (float) dblValProva ;
System.out.println(prova2);

(This is an example that I was working with myself, hope these examples can help others in future)

JSONArray color // This is a JSON array containing 3 values... 
//for instance [ 0.501, 0.501, 0.0 ]
double r = (Double) color.get(0);
double g = (Double) color.get(1);
double b = (Double) color.get(2);
Color3f floorColor = new Color3f((float)r, (float)g, (float)b);

For me this previously was giving errors when I tried to use:

Color3f((float)color.get(0), (float)color.get(1), (float)color.get(2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文