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

asked Nov 18, 2010 at 9:29
2
  • what result are you getting? What types are 'hours' and 'mins' and 'this.hours' ? Commented Nov 18, 2010 at 9:31
  • hours and/or mins need to be double. Are they ? Commented Nov 18, 2010 at 9:33

6 Answers 6

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;
 }
answered Nov 18, 2010 at 9:33
Sign up to request clarification or add additional context in comments.

3 Comments

doesn't understand you answer, and the function from above works for topic starter so I doesn't get your point...
isn't it a little stupid that you give me a downvote if the answer is usefull for the starter?
The OP isn't usually the best judge. See here for a disproof of your answer.
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

answered Nov 18, 2010 at 9:32

2 Comments

how can you see that double and mins are int?
It's more an intuition. The variable names point to integer values (hour and minute) and it's also a common error to think that a division returns double.
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);
answered Nov 18, 2010 at 9:35

Comments

0

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

answered Nov 18, 2010 at 9:33

Comments

0

try using like follow

this.hours = Round(hours + (((double)mins) / 60), 3); 
answered Nov 18, 2010 at 9:36

Comments

-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.

answered Nov 18, 2010 at 10:06

5 Comments

Isn't it obvious that the OP just wants to go from something like 1.333333333 to 1.333?
Yes, and a base-2 double can't hold either of those values.
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 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.
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).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.