0

Hi I'm trying to run a calculation but I can't seem to put it all on one line, I have to split the result into two seperate lines to achieve the desired result (which seems a bit long winded). Can anyone explain where I'm going wrong?

both x and y are doubles.

Example 1: (incorrect)

y=0.68
x= (Math.round(y* 10))/10;
result x=0

Example 2: (correct)

y=0.68
x= Math.round(y* 10);
x = x/10;
result x=0.7

thanks for your time.

3
  • What are the types of x and y? Commented Mar 27, 2012 at 9:43
  • "which seems a bit long winded" Prefer clarity over brevity. Commented Mar 27, 2012 at 9:43
  • 2
    You think x=7 is a correct result ??? Commented Mar 27, 2012 at 9:46

6 Answers 6

1

Math.round returns variable of type long (see: Javadoc), which means that the division by 10 is performed on a long variable resulting in another long variable - that's why you lose the precision.

To make it calculate on it as on double and return double - you have to cast the result of Math.round like this:

x= ((double)Math.round(y* 10))/10;
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried to explicitly specify double in your calculation:

x = ((double)Math.round( y * 10.0)) / 10.0;

Math.round returns a long....

Comments

0

It's hard to tell from your snippets, because they don't include variable types, but it's likely to be integer division that's killing you. When you divide two integers x and y, where x < y, you get zero:

int x = 4;
int y = 10;
int z  = x/y; // this is zero.

Comments

0
y=0.68
x= (Math.round(y* 10)) <--- Evaluated as int since Math.round returns int /10; <-- integer division
result x=0


y=0.68
x= Math.round(y* 10) <-- x is stored as double
x = x/10; <-- double division
result x=7

Comments

0

I guess it's because Math.round returns either a long or an int, depending on whether y is double or float. an then you have an integer division. in the second example x is already a double and that's why you have a double division.

Comments

0

When you write:

double x = (Math.round(y* 10))/10;

(Math.round(y* 10)) is a long (= 7), which you divide by 10, that gives another long (= 0). The result is then converted back to a double and stored in x.

In your second snippet:

double x = Math.round(y* 10);

This is equal to 7 and converted into a double. x / 10 is then a double operation that returns 0.7.

Comments

Your Answer

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.