Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

incorrect float to double convertsion

807607Jan 3 2007 — edited Jan 5 2007
Hi all.

While doing some programming in Java, I came across the following issue:

float a = 23.3
double b = 23.3

if i do

System.out.println(a)

or

System.out.println(b)

I will get: 23.3

So, now I do some castings.

float a = 23.3
double b = a

Doing the same as above with println() I will get:

a = 23.3

BUT

b = 21.2999906...

Now, turning it upside down:

double a = 23.3
float b = (float)a

I will get 23.3 for both, a and b.

So, can anyone tell me what's going on behinde the scenes? So far, I just don't get it :-(


Thanks

Marcel
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2007
Added on Jan 3 2007
18 comments
510 views