Results 1 to 4 of 4
  1. #1
    pinar is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Exclamation Printf problem!!!

    Hi.İ want to format with print f for double cost=1.37*10
    it is output 1.3700001 ı want to 1.37 show help me pls

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please show us how you're trying to use printf.

    Here's an example of using printf to specify precision of displayed double/float value: SpecifyingtheWidthandPrecision
    Last edited by Fubarable; 11-06-2010 at 10:00 PM.

  3. #3
    Johanis is offline Senior Member
    Join Date
    Oct 2010
    Posts
    139
    Rep Power
    0

    Wink

    Quote Originally Posted by pinar View Post
    Hi.İ want to format with print f for double cost=1.37*10
    it is output 1.3700001 ı want to 1.37 show help me pls
    Hi I had the exact problem and I used this code to fix the problem:
    Java Code:
     double resS0 = resS%1;//resS is the number you want rounded
             double resS1 = resS0 * 100;
             double resS2 = Math.round(resS1);
             double resS3 = resS2 / 100;
    good luck!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Johanis View Post
    Hi I had the exact problem and I used this code to fix the problem:
    Java Code:
     double resS0 = resS%1;//resS is the number you want rounded
             double resS1 = resS0 * 100;
             double resS2 = Math.round(resS1);
             double resS3 = resS2 / 100;
    good luck!

    I'm going to disagree here as rounding is not going to solve what is essentially a display problem. e.g.,

    Java Code:
          double foo = 1.37 * 3.5 * 3 * 2;
          foo /= (21);
          System.out.println("cost = " + foo);
          System.out.printf("cost = %.2f%n", foo);

    Of course there is more than one way to skin this cat, and if in fact cost represents a display of currency, then why not use a NumberFormat.getCurrencyInstance?

    Java Code:
       public static void main(String[] args) {
          double cost = 1.37 * 3.5 * 3 * 2;
          cost /= (21);
          System.out.println("cost = " + cost);
          System.out.printf("cost = %.2f%n", cost);
          
          NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
          System.out.printf("cost = %s%n", currencyFormat.format(cost));
       }
    Last edited by Fubarable; 11-06-2010 at 10:59 PM.

Similar Threads

  1. Few errors with for loop/printf
    By ks1615 in forum New To Java
    Replies: 8
    Last Post: 03-12-2009, 10:47 PM
  2. Printf error
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 07:59 AM
  3. printf error with int and double
    By o0n1 in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 09:58 PM
  4. Demonstration of printf() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:37 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 05:31 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
  •