Results 1 to 5 of 5

Thread: Decimal Fill up

  1. #1
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Decimal Fill up

    Hey guys

    Below mentioned is a simple program which i wrote that will decide how many digits to display after decimal place.


    class Exercise1

    {


    public static void main(String[] args)

    {

    double d = 2344.5;

    System.out.println("%.5f", d);

    }

    }

    Now as per the above code the output should be 2344.500000, but instead of that i get an error

    cannot find symbol, symbol: method println(java.lang.String.double)

    location : class java.io.PrintStream

    Can anyone help me out with this one????

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,351
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by maya700 View Post
    System.out.println("%.5f", d);
    You need the System.out.printf( ... ) method, not the System.out.println( ... ) method.

    kind regards,

    Jos

  3. #3
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Tht worked

    Thanks, that worked. So do i have to use printf every time i use %f or %someother????

    Thank you.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,351
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by maya700 View Post
    Thanks, that worked. So do i have to use printf every time i use %f or %someother????

    Thank you.
    You're welcome of course; read the API documentation for the PrintStream class (System.out is a PrintStream object). The print( ... ) and println( ... ) methods take a single argument while printf( ... ) takes many arguments and uses a Formatter to do the job. The API documents are your friend.

    kind regards,

    Jos

  5. #5
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

Similar Threads

  1. fill table with cells
    By BigBear in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2010, 09:22 PM
  2. How to Fill Arc in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:14 PM
  3. How to Fill a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM
  4. How to Fill a Polygon in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM
  5. Array Fill Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 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
  •