Convert double to float in Java

前端 未结 10 822
执念已碎
执念已碎 2020-12-08 18:13

I am facing an issue related to converting double to float. Actually, I store a float type, 23423424666767, in a database, but when we

相关标签:
10条回答
  • 2020-12-08 18:32

    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.

    0 讨论(0)
  • 2020-12-08 18:36

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

    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.

    0 讨论(0)
  • 2020-12-08 18: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

    0 讨论(0)
  • 2020-12-08 18:37

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 18:42

    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;
    
    0 讨论(0)
  • 2020-12-08 18:44

    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:

    double d = 0.5;
    Double D = Double.valueOf(d);
    float f = D.floatValue();
    
    0 讨论(0)
提交回复
热议问题