1

I'm searching for the best practice to convert Float to Double without loosing precision. So far I only found that a proper way to do so is to convert the Float to String and the String to Double.

Searching the Float API I stumbled upon this method doubleValue(). I thought this is a static constructor that will return a double from my Float without loosing precision but the following code behaves like a cast:

public class Main {
 public static void main(String[] args) {
 Float floatNumber= 4.95f;
 Double doubleNumber= floatNumber.doubleValue();
 System.out.println(doubleNumber);
 }
}

The output is 4.949999809265137

Searching any other documentation about this from the Float API I didn't find any documentation to tell me what exactly happens when I call that method. Does anybody have any idea? Or can someone confirm that all the method does is perform a cast my Float to a double and unbox it?

mskfisher
3,4124 gold badges38 silver badges50 bronze badges
asked Nov 18, 2011 at 10:47

3 Answers 3

7

The simple primitive-type cast (or even an implicit conversion) will do all you need, if you really want to preserve the value of a float:

float f = 4.95f;
double d = f;

Fundamentally 4.95f is already inaccurate - you can't represent that number exactly as a float. The exact value of floatNumber is represented in doubleNumber too - it's just that that value is not 4.95.

If you really care about the exact decimal digits, you should be using BigDecimal instead (or a scaled integer).

answered Nov 18, 2011 at 10:49
Sign up to request clarification or add additional context in comments.

3 Comments

I was about to say that and suggest BigDecimal, but as usual you beat me to it. Do you live on StackOverflow? Are you even human or a SO bot? ;)
I don't have the option of using BigDecimal. I don't want to preserve the actual value of the float. I want to treat is as that would be double after the value was set to float in another system to which I don't have access to modify its type (I can only receive it as float). And lastly what does Float.doubleValue() actually do?
@Romeo: The actual value of the float is 4.94999980926513671875. That's the nearest exact float value to the original 4.95. So it looks like it's doing the right thing. As for what Float.doubleValue() does - it's basically the normal conversion from float to double, which is safe as every float is exactly representable as a double.
2

Widening float to double conversion doesn't lose precision in java.

answered Nov 18, 2011 at 10:50

2 Comments

Try that piece of code in the post. It doesn't lose precision, but from a human POV that number is not what I would have expected to be.
@Romeo: That's because you're not storing it in a type which preserves a human POV - you're storing it in a binary floating point type.
0

When you use floating point, you get rounding errors which can be potentially "corrected" by rounding the result (assuming you know the precision you want). BigDecimal handles this by always knowing the precision.

float floatNumber= 4.95f;
double doubleNumber = (double) floatNumber;
System.out.println("After cast " + doubleNumber);
System.out.printf("Show two decimal places. %.2f%n", doubleNumber);
doubleNumber = Math.round(doubleNumber * 100) / 100.0;
System.out.println("After rounding to two places " + doubleNumber);

prints

After cast 4.949999809265137
Show two decimal places. 4.95
After rounding to two places 4.95
answered Nov 18, 2011 at 11:29

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.