Results 1 to 3 of 3
  1. #1
    pinar is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default this programme dont work as I want Help me!!

    this is true output of programme /*4.3 (Conversion from kilograms to pounds) Write a program that displays the following table (note that 1 kilogram is 2.2 pounds):
    Kilograms Pounds
    1 2.2
    3 6.6

    197 433.4
    199 437.8*/

    but ı wrote
    System.out.println("kilogram" + " pound");
    for(int i=1;i<=199;i=i+2)

    System.out.println(i + "" + i*2.2);


    this is my output

    1 2.2
    3 6.6000000000000005
    5 11.0
    7 15.400000000000002
    9 19.8
    11 24.200000000000003
    13 28.6

    I didnt understand 6.60000000000005 instead of 6.6
    or 15.400000000002 instead of 15.4

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    When you display a numeric value like i*2.2 on the screen as a string you have to decide on the formatting and other properties of the string.

    At the moment you are using the println() method of System.out but there is another method you can use: printf().

    It can be a bit tricky to start with, but try

    Java Code:
    System.out.printf("%d %.2f%n", i, i*2.2);

    "%d %.2f%n" is a format string. %d means "print a decimal integer", %.2f means "print a floating point number with 2 decimal places", %n means "print a new line".

    Full details in the Formatter (Java Platform SE 6) Formatter API docs. There's a lot to digest here - but it's worth reading up on what you need on a particular occasion and going back to it as required.


    A completely different approach is to use the DecimalFormat class.

    Both approaches are covered in Oracle's Tutorial in the section Formatting Numeric Print Output.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Waterford, Ireland
    Rep Power


    double calculations are fairly precise, you will have to format the output
    String (Java Platform SE 6)

    I keep getting beaten to it.....
    Last edited by al_Marshy_1981; 10-26-2010 at 11:29 PM. Reason: pbrockway explained it

Similar Threads

  1. cannot compile the programme
    By Roshini in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 11:02 AM
  2. Can I embed a console java programme on my site?
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 08-10-2010, 08:14 PM
  3. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  4. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 11:24 AM
  5. add java programme on windowsXP right-click menu
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 07-22-2008, 04:20 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