简体   繁体   English

在Java中将double转换为float

[英]Convert double to float in Java

I am facing an issue related to converting double to float .我面临与将double转换为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 double type.实际上,我在数据库中存储了一个浮点型23423424666767 ,但是当我们在下面的代码getInfoValueNumeric()从数据库中获取数据时,它是double getInfoValueNumeric()型。 The value we get is in the 2.3423424666767E13 form.我们得到的值是2.3423424666767E13形式。

So how do we get a float format data like 23423424666767 ?那么我们如何获得像23423424666767这样的float格式数据呢?

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);
}

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

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 为您提供了很多功能。

Convert Double to FloatDouble Float转换为Float

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

Convert double to Floatdouble Float转换为Float

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

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();

To answer your query on "How to convert 2.3423424666767E13 to 23423424666767"回答您关于“如何将 2.3423424666767E13 转换为 23423424666767”的问题

You can use a decimal formatter for formatting decimal numbers.您可以使用十进制格式器来格式化十进制数。

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

Output : 23423424666767输出:23423424666767

Converting from double to float will be a narrowing conversion.从 double 转换为 float 将是一种缩小转换。 From the doc :文档

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).从 double 到 float 的缩小原语转换受 IEEE 754 舍入规则(第 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.双 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;

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.另一件事是:您没有得到“2.3423424666767E13”,它只是存储在内存中的数字的直观表示。 "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.上面的示例向您展示了如何将数字打印为浮点数,从而避免使用科学记数法。

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 .首先,数据库中的值是浮点数这一事实并不意味着它也适合 Java float Float is short for floating point , and floating point types of various precisions exist. Float 是floating point 的缩写,存在各种精度的浮点类型。 Java types float and double are both floating point types of different precision. Java 类型floatdouble都是不同精度的浮点类型。 In a database both are called FLOAT .在数据库中,两者都称为FLOAT Since double has a higher precision than float , it probably is a better idea not to cast your value to a float , because you might lose precision.由于doublefloat具有更高的精度,因此最好不要将您的值转换为float ,因为您可能会失去精度。

You might also use BigDecimal , which represent an arbitrary-precision number.您也可以使用BigDecimal ,它表示一个任意精度的数字。

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, eg 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).双精度数是一种特定类型的浮点数,其精度高于标准浮点数(意味着它们可以精确到更多的小数位)。

Float.parseFloat(String.valueOf(your_number)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM