Results 1 to 7 of 7
  1. #1
    consurgens is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default strange outcome!

    public class Test {
    public static void main(String[] args) {
    double n = 20.5 - 20.37;

    System.out.println("n = " + n);
    }
    }

    With this code the outcome should be n = 0.13 but instead, it displays 0.129999999999999

    I don't know what wrong with this code.
    Could anyone explain me what happen?

    Thank you ^^

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

  3. #3
    consurgens is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank you for reply

    I've just studied programming language and I have chosen Java as primary language. So I don't know much about programming. Could you show me coding that make this outcome as 0.13?

    Thank you.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Have a look at the DecimalFormat API.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    consurgens is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    import java.text.*;

    public class Test {
    public static void main(String[] args) {

    DecimalFormat df = new DecimalFormat("0.00");
    double n = 20.5 - 20.37;

    System.out.println(df.format(n));
    }
    }

  6. #6
    consurgens is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank for your recommendation

    ^^

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

Similar Threads

  1. strange problem
    By dinosoep in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 10:28 AM
  2. Strange Error
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 09:31 PM
  3. Strange Problem
    By Aseem in forum JDBC
    Replies: 5
    Last Post: 02-12-2010, 07:46 AM
  4. strange swt exception
    By ahmed.kotb in forum SWT / JFace
    Replies: 0
    Last Post: 04-23-2009, 10:15 AM
  5. Outcome differ from Netbeans and Java Web Start
    By bongia in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 01:35 PM

Posting Permissions

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