gpt4 book ai didi

java - 将 double 转换为 float

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:00 24 4
gpt4 key购买 nike

我看过一个示例,其中使用以下代码将 double 转换为 float :

Double.valueOf(someDouble).floatValue()

我会这样做

(float)someDouble

使用前者有什么优势吗?

最佳答案

查看 DoublefloatValue() 的实现:

/**
* Returns the value of this {@code Double} as a {@code float}
* after a narrowing primitive conversion.
*
* @return the {@code double} value represented by this object
* converted to type {@code float}
* @jls 5.1.3 Narrowing Primitive Conversions
* @since JDK1.0
*/
public float floatValue() {
return (float)value;
}

看起来它的行为与您的类型转换完全一样。因此,使用它没有任何优势。

关于java - 将 double 转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42109916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com