在Java中将double转换为float

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32837783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:11:16  来源:igfitidea点击:

Convert double to float in Java

javadoublefloating

提问by Sitansu

I am facing an issue related to converting doubleto float. Actually, I store a float type, 23423424666767, in a database, but when we get data from the database in the below code, getInfoValueNumeric(), it's of doubletype. The value we get is in the 2.3423424666767E13form.

我面临与转换doublefloat. 实际上,我23423424666767在数据库中存储了一个浮点类型 , ,但是当我们在下面的代码中从数据库中获取数据时getInfoValueNumeric(),它是double类型的。我们得到的值是2.3423424666767E13形式。

So how do we get a floatformat data like 23423424666767?

那么我们如何获得float像这样的格式数据23423424666767呢?

2.3423424666767E13 to 23423424666767

2.3423424666767E13 to 23423424666767

public void setInfoValueNumeric(java.lang.Double value) {
    setValue(4, value);
}


@javax.persistence.Column(name = "InfoValueNumeric", precision = 53)
public java.lang.Double getInfoValueNumeric() {
    return (java.lang.Double) getValue(4);
}

采纳答案by Tom Wellbrock

Just cast your double to a float.

只需将您的 double 转换为浮点数即可。

double d = getInfoValueNumeric();
float f = (float)d;

Also notice that the primitive types can NOT store an infinite set of numbers:

还要注意,原始类型不能存储无限的数字集:

float range: from 1.40129846432481707e-45 to 3.40282346638528860e+38
double range: from 1.7e–308 to 1.7e+308

回答by antoniodvr

I suggest you to retrieve the value stored into the Database as BigDecimal type:

我建议您以 BigDecimal 类型检索存储在数据库中的值:

BigDecimal number = new BigDecimal("2.3423424666767E13");

int myInt = number.intValue();
double myDouble = number.doubleValue();

// your purpose
float myFloat = number.floatValue();

BigDecimal provide you a lot of functionalities.

BigDecimal 为您提供了很多功能。

回答by Priyambada Madala

To answer your query on "How to convert 2.3423424666767E13 to 23423424666767"

回答您关于“如何将 2.3423424666767E13 转换为 23423424666767”的问题

You can use a decimal formatterfor formatting decimal numbers.

您可以使用十进制格式器来格式化十进制数。

     double d = 2.3423424666767E13;
     DecimalFormat decimalFormat = new DecimalFormat("#");
     System.out.println(decimalFormat.format(d));

Output : 23423424666767

输出:23423424666767

回答by piezol

The problem is, your value cannot be stored accurately in single precision floating point type. Proof:

问题是,您的值无法以单精度浮点类型准确存储。证明:

public class test{
    public static void main(String[] args){
        Float a = Float.valueOf("23423424666767");
        System.out.printf("%f\n", a); //23423424135168,000000
        System.out.println(a);        //2.34234241E13
    }
}

Another thing is: you don't get "2.3423424666767E13", it's just the visual representation of the number stored in memory. "How you print out" and "what is in memory" are two distinct things. Example above shows you how to print the number as float, which avoids scientific notation you were getting.

另一件事是:您没有得到“2.3423424666767E13”,它只是存储在内存中的数字的直观表示。“您如何打印”和“内存中的内容”是两件不同的事情。上面的示例向您展示了如何将数字打印为浮点数,从而避免使用科学记数法。

回答by akhil_mittal

Converting from double to float will be a narrowing conversion. From the doc:

从 double 转换为 float 将是一种缩小转换。从文档

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity.

缩小原语转换可能会丢失有关数值整体大小的信息,也可能会丢失精度和范围。

从 double 到 float 的缩小原语转换受 IEEE 754 舍入规则(第 4.2.4 节)的约束。这种转换可能会失去精度,但也会失去范围,从而导致非零双精度浮点数为零,有限双精度数产生浮点无穷大。双 NaN 转换为浮点 NaN,双无穷大转换为同号浮点无穷大。

So it is not a good idea. If you still want it you can do it like:

所以这不是一个好主意。如果你仍然想要它,你可以这样做:

double d = 3.0;
float f = (float) d;

回答by Hoopje

First of all, the fact that the value in the database is a float does not mean that it also fits in a Java float. Float is short for floating point, and floating point types of various precisions exist. Java types floatand doubleare both floating point types of different precision. In a database both are called FLOAT. Since doublehas a higher precision than float, it probably is a better idea notto cast your value to a float, because you might lose precision.

首先,数据库中的值是浮点数这一事实并不意味着它也适合 Java float。Float 是floating point 的缩写,存在各种精度的浮点类型。Java 类型floatdouble都是不同精度的浮点类型。在数据库中,两者都被称为FLOAT。由于double具有比 更高的精度float,因此最好不要将您的值转换为 a float,因为您可能会失去精度。

You might also use BigDecimal, which represent an arbitrary-precision number.

您也可以使用BigDecimal,它代表一个任意精度的数字。

回答by Akash Middinti

Float.parseFloat(String.valueOf(your_number)

Float.parseFloat(String.valueOf(your_number)

回答by Phan Van Linh

Convert Doubleto Float

转换DoubleFloat

public static Float convertToFloat(Double doubleValue) {
    return doubleValue == null ? null : doubleValue.floatValue();
}

Convert doubleto Float

转换doubleFloat

public static Float convertToFloat(double doubleValue) {
    return (float) doubleValue;
}

回答by wpmoradi

Use dataType casting. For example:

使用数据类型转换。例如:

// converting from double to float:
double someValue;
// cast someValue to float!
float newValue = (float)someValue;

Cheers!

干杯!

Note:

笔记:

Integers are whole numbers, e.g. 10, 400, or -5.

整数是整数,例如 10、400 或 -5。

Floating point numbers (floats) have decimal points and decimal places, for example 12.5, and 56.7786543.

浮点数(浮点数)有小数点和小数位,例如 12.5 和 56.7786543。

Doubles are a specific type of floating point number that have greater precision than standard floating point numbers (meaning that they are accurate to a greater number of decimal places).

双精度数是一种特定类型的浮点数,其精度高于标准浮点数(意味着它们可以精确到更多的小数位)。

回答by Alon Gweta

This is a nice way to do it:

这是一个很好的方法:

`Double d = 0.5;`
`float f = d.floatValue();`

if you have d as a primitive type just add one line:

如果您将 d 作为原始类型,只需添加一行:

double d = 0.5;
`Double D = Double.valueOf(d);`
`float f = D.floatValue();`