0

Hey guys, I am trying to round to 3 decimal places.

I used the following code.

this.hours = Round(hours + (mins / 60), 3);

But it's not working. Where have I gone wrong? Thanks

sark9012
  • 5,485
  • 18
  • 61
  • 99

6 Answers6

5

You can use this function:

 public static double Round(double number, int decimals)
    {
    double mod = Math.pow(10.0, decimals);
    return Math.round(number * mod ) / mod;
    }
Michel
  • 9,220
  • 13
  • 44
  • 59
  • 1
    doesn't understand you answer, and the function from above works for topic starter so I doesn't get your point... – Michel Nov 18 '10 at 10:49
  • isn't it a little stupid that you give me a downvote if the answer is usefull for the starter? – Michel Nov 19 '10 at 08:16
  • The OP isn't usually the best judge. See [here](http://stackoverflow.com/a/12684082/207421) for a disproof of your answer. – user207421 Nov 23 '12 at 11:48
2

First thing is that all your variables are int so the result of your division is also an int, so nothing to Round.

Then take a look to: How to round a number to n decimal places in Java

Community
  • 1
  • 1
Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
2

If mins is an integer, then mins / 60 will result in an integer division, which always results in 0.

Try changing from 60 to 60.0 to make sure that the division is treated as a floating point division.

Example:

int hours = 5;
int mins = 7;

// This gives 5.0
System.out.println(Math.round(1000 * (hours + (mins / 60  ))) / 1000.0);

// While this gives the correct value 5.117.           (.0 was added)
System.out.println(Math.round(1000 * (hours + (mins / 60.0))) / 1000.0);
aioobe
  • 413,195
  • 112
  • 811
  • 826
0

if mins is an integer you have to divide through 60.0 to get a floating number which you can round

christian
  • 391
  • 2
  • 10
0

try using like follow

this.hours = Round(hours + (((double)mins) / 60), 3); 
asela38
  • 4,546
  • 6
  • 25
  • 31
-1

You can't. Doubles don't have decimal places, because they are binary, not decimal. You can convert it to something that does have decimal places, i.e. a base-ten number, e.g. BigDecimal, and adjust the precision, or you can format it for output with the facilities of java.text, e.g. DecimalFormat, or the appropriate System.out.printf() string.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Isn't it obvious that the OP just wants to go from something like `1.333333333` to `1.333`? – aioobe Nov 18 '10 at 10:59
  • Yes, and a base-2 double can't hold either of those values. – user207421 Nov 18 '10 at 11:42
  • @downvoter Please explain, and please see [here](http://stackoverflow.com/a/12684082/207421) for proof. – user207421 Nov 23 '12 at 11:49
  • The number stored by the computer represents a mathematical number. It's perfectly fine to discuss the decimal representation of this number, and to drop all decimals except the three first is AFAIK a well defined operation. Indeed you may not be able to store the result in an ordinary double, but I'm sure that the rounding required at that point is acceptable by the OP. (Hey, why don't you ask him in the comment section below the question.) All in all however, I think you got the downvote due to your "You can't" part, which you perhaps wrote due to a misunderstanding of the question. – aioobe Nov 23 '12 at 14:52
  • @aioobe The answer is correct for the reasons I gave, which you have not addressed. The only 3-decimal-digit fractions that can be stored accurately in a double are 0.125, 0.375, 0.625, and 0.875. All others are approximations. It not only incorrect but meaningless to talk of rounding a floating point number to 3 decimal places. Refer to the link for proof. If you have a counterproof of my proof, let's have it. At least, tell us exactly where the decimal digits are stored in a floating point value. Without that you are just wasting time. – user207421 Nov 23 '12 at 21:36
  • I disagree. It's not meaningless to talk of rounding a floating point number to 3 decimal places, since you can approximate it (which is actually exactly what the OP is after in this case). – aioobe Nov 24 '12 at 09:54