Results 1 to 3 of 3

Thread: Double

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

    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
    Posts
    1,576
    Rep Power
    8

    Default

    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
    Location
    Chennai
    Posts
    396
    Rep Power
    6

    Default

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


    Regards
    pandian

Similar Threads

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