Results 1 to 8 of 8
  1. #1
    benhawk is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 3 Decimal Places

    How can i make a double display to a maximum of 3 decimal places without rounding it up?
    Last edited by benhawk; 11-02-2010 at 10:45 PM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    You're going to need to show more information, as I don't know where those numbers at the bottom are coming from, or really what you are trying to do. Maybe post what your test data is or better yet your test class.

    In your setHoursWorked(int, int) make sure you either typecast the min variable to a double or the 60 to a double(make it 60d). I don't know why you are taking the calculation to the third power...

    Try to keep everything in one unit of measurement, preferably the smallest. So i would suggest storing everything as minutes.

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Don't round at all - unless the specifications you are working against specifically say you must. Rounding can only make things less accurate. If the user enters "1 hour and 10 minutes" then the double value should be the result of "1+10/60.0" whatever that value may be.

    For displaying the value, consider using the various Formatter format strings:

    Java Code:
    double hours = 1 + 10 / 60.0;
    System.out.printf("hours = %.3f%n", hours);
    System.out.println("or " + String.format("%.3f", hours));

    Details of the % format strings are in the API docs linked to above.

    [Edit] Another way to avoid rounding is StormyWaters suggestion of using minutes throughout. You might still need to do the hours calculation and formatting for output though.
    Last edited by pbrockway2; 11-02-2010 at 09:51 PM.

  4. #4
    benhawk is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    Unfortunately it has to been in that exact way :( which does make it a pain, the value also has to be returned into the double hours, so i cant use a String :(

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I would still suggest storing the time in only minutes. You can easily calculate out the hours using a method.

    Java Code:
    private int mTime;
    
    public void setHoursWorked(int hours, int minutes) {
        mTime = minutes;
        mTime += (hours * 60);
    }
    
    public double getHours() {
         return (mTime / 60d);
    }
    
    public int getMinutes() {
         return (mTime % 60);
    }
    To format the String representation of the Hours worked, use the DecimalFormat class. It contains methods to set the maximum and minimum number of decimal places for the return String.

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    I think this solves your problem

    Java Code:
    private static double truncate(double x){
      if ( x > 0 )
        return Math.floor(x * 100)/100;
      else
        return Math.ceil(x * 100)/100;
    }
    Found at Java - truncate to 2 decimals places

    Cheers,
    Erik

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I've heard tell that people attempt to round to 3dp by multiplying by 1000 (that's where the Math.pow() comes in). Then truncate or round to an integer value. Then divide by 1000.

    -----

    To repeat (and add some more): it's ugly, it's inaccurate, it's unnecessary (I more and more like the minutes idea) and it may not do what you expect.

    Format strings nicely for human consumption by all means. But going to extra work to store a value like hours in a way that is inaccurate (because it's a double and because it's rounded) seems a bit perverse.

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    in java there is a class named NumberFormat with a large quantity of methods that helps you format and also parsing numbers for any locale. so use it! here is small example to fix the problem with fractional digits:


    Java Code:
    import java.text.NumberFormat;
    
    public class NumberFormatExample {
    	public static void main(String[] args) {
    		Double d = 123.45678d;
    		NumberFormat nf = NumberFormat.getInstance();
    		// here you can set the number of fractional digits
    		nf.setMaximumFractionDigits(2);
    		System.out.println(nf.format(d));
    		nf.setMaximumFractionDigits(3);
    		System.out.println(nf.format(d));
    	}
    }

    note that the setMaximumFractionDigits() method doesn't cut the digits but rounds it to the value given. for the above code the output is

    123.46
    123.457

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 03:01 AM
  2. 2 decimal places needed
    By lala in forum New To Java
    Replies: 5
    Last Post: 10-27-2010, 09:13 PM
  3. [SOLVED] Decimal Places - Is this possible?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-08-2009, 08:49 PM
  4. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-28-2008, 12:04 AM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 10:28 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
  •