简体   繁体   中英

Java why doesn't my calculation work on this code?

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.

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;

Have you tried to explicitly specify double in your calculation:

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

Math.round returns a long....

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM