Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I am new to java -could you please tell me why output appearing like this?????

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    Default I am new to java -could you please tell me why output appearing like this?????

    Below is the program i wrote.

    class Value
    public static void main(String [] args)
    float v=2.234567f;
    System.out.println("the valu of v="+v);

    1) When i give the valu of v=2.234567f provide output as "the valu of v=2.234567"
    2)When i give the value of v=2.2345678f it provides output as"the valu of v=2.2345679" [ i thought the ouput will 2.2345678]
    3)When i give the value of v=2.2345679f it provides output as "the valu of v=2.2345679"[my doubt is for the value of v in step 2 and the value of v in step 3 gives the same output why??].

    Thanks in advance for clarifying and looking into my doubt.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 632 Times in 540 Posts

    Default Re: I am new to java -could you please tell me why output appearing like this?????

    Google: "what every computer scientist should know about floating point arithmetic".

    The gist: computers have trouble representing exact decimals in binary. You're seeing the expected behavior.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: July 7th, 2011, 03:46 PM
  2. Format Java output
    By wildman0501 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 11th, 2011, 06:09 PM
  3. The java-output doesn't give the right result?
    By pinotje in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 31st, 2011, 01:00 PM
  4. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM
  5. Output String Handling in Java
    By merry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2010, 08:59 PM