Results 1 to 3 of 3

Thread: Double

  1. #1
    Xystus777 is offline Member
    Join Date
    Jan 2009
    Rep Power

    Exclamation Double

    Hello everyone,

    I have a program which does some stuff for a payroll (this is only an exercise for me).

    Well, I'm using doubles as my variable Type. However, whenever i'm displaying the money, it is showing the actual pay, which is showing 3 decimal places. How can I eliminate all of the decimals past the 2nd?

    Thanks! Please remember I'm a beginner!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    We have the String.format method and the Formatter class in j2se 1.5+:
    Java Code:
            double d = 1.0/3.0;
            System.out.println("d = " + d);
            String s = String.format("%.2f", d);
            System.out.println("s = " + s);
            // Another way.
            System.out.printf("formatted d = %.2f%n", d);
            // Trick not recommended for $
            double f = ((int)(d * 100))/100;
            System.out.println("f = " + f);
    Before j2se 1.5 we had the NumberFormat and DecimalFormat classes.

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    do u know C language?
    in c we can use printf() function that satisfy your the same manner we can use system.out.printf() in java.
    before use this function u had to revisit C printf () function


Similar Threads

  1. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM
  2. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 08:05 AM
  3. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 01:11 PM
  4. Divison - Double
    By new person in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 11:01 PM
  5. Double Buffering
    By dunff2k in forum SWT / JFace
    Replies: 1
    Last Post: 11-14-2008, 04:26 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