Results 1 to 9 of 9
  1. #1
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default Java Algorithm problems?

    Right, since im a newbie in java please be nice :)

    Look at following 2 statements in java code:
    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)
    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...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. ;)

  3. #3
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    Whats VM????? :confused:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    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.

  6. #6
    Alan-LB's Avatar
    Alan-LB is offline Member
    Join Date
    Jun 2008
    Location
    Junee, NSW, Australia
    Posts
    19
    Rep Power
    0

    Default

    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!!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Hey Alan, can't you add links in such situations. :) It's better to add a link to the URL when you posting links.

  8. #8
    Alan-LB's Avatar
    Alan-LB is offline Member
    Join Date
    Jun 2008
    Location
    Junee, NSW, Australia
    Posts
    19
    Rep Power
    0

    Default

    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!!

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Algorithm statement in my Java codes
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-24-2008, 02:52 PM
  2. Soundex Algorithm Implementation in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:40 PM
  3. Using Java To Implement RSA Algorithm
    By Floetic in forum New To Java
    Replies: 3
    Last Post: 03-31-2008, 11:56 PM
  4. textfile with Java problems
    By saytri in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:16 PM
  5. Help with algorithm in java
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-01-2007, 06:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •