View Single Post
  #1 (permalink)  
Old 07-01-2008, 12:00 PM
JavaInLove JavaInLove is offline
Member
 
Join Date: Feb 2008
Posts: 8
JavaInLove is on a distinguished road
Java Algorithm problems?
Right, since im a newbie in java please be nice

Look at following 2 statements in java code:
Code:
System.out.println(456.8 + 0.09); //456.89 (WORKS as expected) System.out.println(23.4 + 0.09); //23.49 (WORKS as expected) System.out.println(1.8 + 0.09); //1.8900.....01 (DOESNT WORK as expected)
i've been told that the above situation happens because of floating points and computer calculations and not an error.

So what i want to discuss is that how do i rectify it? And what is the best way to solve it so that i want 1.89 to be displayed instead of ...

Im writing a calculator so i need 1.89 to be displayed on screen rather than 1.8900..01. Currently those display values are held in a variable of double.
DO i change it to float or something else so that it can accept a bigger range?

Any help would be greatful! Thanks in advance...
Reply With Quote
Sponsored Links