113

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);
}
Bhargav Rao
52.6k29 gold badges130 silver badges142 bronze badges
asked Sep 29, 2015 at 7:09
5
  • 4
    just cast it. double d = 3.0; float f = (float) d; Commented Sep 29, 2015 at 7:11
  • The numbers look the same, just different representations of the same value. Commented Sep 29, 2015 at 7:13
  • Its not working for me Commented Sep 29, 2015 at 7:13
  • if its the formatting you wish to archieve use the NumberFormat libary. If you want to shorten your number. Like: 10000->100 you could use math.round() Commented Sep 29, 2015 at 7:18
  • float doesn't have enough precision for such numbers like 23423424666767. It can only be precise to ~7 decimal digits Commented Sep 29, 2015 at 7:59

10 Answers 10

237

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
answered Sep 29, 2015 at 7:13
Sign up to request clarification or add additional context in comments.

4 Comments

@JimmyKane make sure you use the primitive types "float" and "double" and not the object types "Float", "Double".
Hey, Tom is this an expensive operation. Can you explain what Java is doing under the hood?
@SamOrozco here are basically two things that happen: 1. the double value is autoBoxed to its object type. It is basically a new instance of Double that hold the primitive double value. 2. As the objectTypes Double and Float are both derived from Number they can be converted into each other.
Now to come back to your question, as we narrow down our type from Double to Float instead of going broader eg. Double to Object. Java has to check for potential ClassCastExceptions at runtime. But I can't really say how much of an Overhead that produces. As for the calculation itself it is nothing more than creating Object instances (autoboxing) and retrieving the primitive Value (autoUnboxing). I know my answer is vague, but I hope it helps none the less. If you are really interested check out Topics related to auto-(un)boxing here at Stackoverflow.
28

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;
}
answered Nov 23, 2017 at 2:01

Comments

27

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.

answered Sep 29, 2015 at 7:14

2 Comments

If anyone is passing a value that is not a String, it's recommended to use BigDecimal.valueOf() instead of new BigDecimal(). See https://dev.eclipse.org/sonar/rules/show/squid:S2111
That old link is dead. Here is another: sonar.spring.io/coding_rules#rule_key=squid%3AS2111
23

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();
answered Sep 18, 2019 at 9:17

1 Comment

Very elegant. I feel safer using this method than just casting --even if it is false security.
5

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;
answered Sep 29, 2015 at 7:27

Comments

3

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

answered Sep 29, 2015 at 7:20

3 Comments

This does not convert a double value to float... format method return a String object.
well afterwards the String can be parsed without much effort
You can always convert a string value to float by 'Float.parseFloat(decimalFormat.format(d))' . Although it still represent the value with scientific representation as 2.34234241E13 .
0

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.

answered Sep 29, 2015 at 7:24

Comments

0

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 float and double are both floating point types of different precision. In a database both are called 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.

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

answered Sep 29, 2015 at 7:55

Comments

0

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.

Floating point numbers (floats) have decimal points and decimal places, for example 12.5, and 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).

answered Oct 25, 2018 at 20:47

Comments

-2

Float.parseFloat(String.valueOf(your_number)

answered Jul 24, 2016 at 11:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.