# 3 Decimal Places

• 11-02-2010, 09:31 PM
benhawk
3 Decimal Places
How can i make a double display to a maximum of 3 decimal places without rounding it up?
• 11-02-2010, 09:47 PM
StormyWaters
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.
• 11-02-2010, 09:48 PM
pbrockway2
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:

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.

 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.
• 11-02-2010, 09:56 PM
benhawk
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 :(
• 11-02-2010, 10:02 PM
StormyWaters
I would still suggest storing the time in only minutes. You can easily calculate out the hours using a method.

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.
• 11-02-2010, 10:02 PM
venerik
I think this solves your problem

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
• 11-02-2010, 10:08 PM
pbrockway2
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.
• 11-02-2010, 10:28 PM
j2me64
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:

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

Quote:

123.46
123.457