Results 1 to 3 of 3
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default force a double to 2 decimal places

    I am doing a few math calculations, and my return type is a double, but it keeps coming back as ##.0

    I have tried to use String.format("%.2f", myDouble) but its still only returning a single 0. Any ideas?

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

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    You can use DecimalFormat class for this.

    e.g.-->
    Java Code:
    double d = 123.984212;
    		
    		DecimalFormat format = new DecimalFormat();
    		format.setMaximumFractionDigits(4);
    		System.out.println(format.format(d));
    Output:
    123.9842

Similar Threads

  1. keep double accurate to two decimal places
    By africanhacker in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 06:23 PM
  2. 3 Decimal Places
    By benhawk in forum New To Java
    Replies: 7
    Last Post: 11-02-2010, 09:28 PM
  3. Limiting decimal places in a double
    By darek9576 in forum New To Java
    Replies: 5
    Last Post: 03-14-2010, 01:13 AM
  4. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  5. Replies: 4
    Last Post: 03-03-2009, 12:43 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
  •