Convert double to float in Java

2019-01-17 19:23发布

I am facing an issue related to converting double to 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. The value we get is in the 2.3423424666767E13 form.

So how do we get a float format data like 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);
}

9条回答
做自己的国王
2楼-- · 2019-01-17 19:34

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.

查看更多
别忘想泡老子
3楼-- · 2019-01-17 19:36

To answer your query on "How to convert 2.3423424666767E13 to 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

查看更多
Anthone
4楼-- · 2019-01-17 19:43

Converting from double to float will be a narrowing conversion. 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). 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.

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;
查看更多
做个烂人
5楼-- · 2019-01-17 19:46

Float.parseFloat(String.valueOf(your_number)

查看更多
beautiful°
6楼-- · 2019-01-17 19:50

Convert Double to Float

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

Convert double to Float

public static Float convertToFloat(double doubleValue) {
    return (float) doubleValue;
}
查看更多
够拽才男人
7楼-- · 2019-01-17 19:54

Just cast your double to a float.

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
查看更多
登录 后发表回答