Results 1 to 9 of 9
Thread: Java Algorithm problems?
- 07-01-2008, 10:00 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
Java Algorithm problems?
Right, since im a newbie in java please be nice :)
Look at following 2 statements in java code:
i've been told that the above situation happens because of floating points and computer calculations and not an error.Java 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)
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...
- 07-01-2008, 10:30 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In default if you use a decimal number VM take it as a double value. If you define them as a float value, you get the correct value.
And see the number of decimal you get, in your code. ;)
- 07-01-2008, 10:55 AM #3
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
Whats VM????? :confused:
- 07-01-2008, 11:05 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
VM - Virtual Machine (java virtual machine normally call like that)
- 07-01-2008, 04:19 PM #5
There are several ways to format the values of float and doubles for display using java classes. For example DecimalFormat
If you read your text you should see that double has a larger range than float. But your problem is displaying the value, so you need to format it to what you want to see.
- 07-02-2008, 08:24 AM #6
You can use System.out.printf to format numbers. This would leave off the last few decimal positions.
System.out.printf("%10.5f\n", 1.8 + .09);
java(dot)sun(dot)com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.l ang.Object...)
Alan.There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
- 07-02-2008, 08:30 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hey Alan, can't you add links in such situations. :) It's better to add a link to the URL when you posting links.
- 07-02-2008, 08:44 AM #8
As long as the OP can find his way to the reference I wanted him to see.
I am told I can't post links or images - so I posted the link my way!!!
Alan.There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
- 07-02-2008, 08:50 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Oki, don't worry in near future we may be fix that issue. You know that, at the time we are discussing this.
Similar Threads
-
Algorithm statement in my Java codes
By javanewbie in forum New To JavaReplies: 2Last Post: 06-24-2008, 02:52 PM -
Soundex Algorithm Implementation in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:40 PM -
Using Java To Implement RSA Algorithm
By Floetic in forum New To JavaReplies: 3Last Post: 03-31-2008, 11:56 PM -
textfile with Java problems
By saytri in forum New To JavaReplies: 4Last Post: 12-29-2007, 05:16 PM -
Help with algorithm in java
By coco in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 06:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks