简体   繁体   中英

Java math logic error

I have the following code in my project:

  int percent = 2;
  int count = 10;
  int percentagefill = (percent/10)*count; 
  System.out.println(percentagefill);

Basically what is happening is that, I'm setting two variables, percent and count. I then calculate the percentage fill. For some strange reason the percentage fill is resulting in a 0, when in this case it should be 2. Any ideas why? Thanks in advance.

int divided by int will still result in int . In this case:

(percent/10)*count
= (2/10)*10
= (0) * 10 <-- 0.2 is rounded down to 0
= 0

You can read this question for reference. Also, here's the Java spec where is says that integer division is rounded towards 0. As for the fix, as long as floating point precision does not become an issue , just use double as PaulP.RO said.

You could divide by 10.0 , or change int percent to double percent in order to force a conversion to double . Otherwise, you are getting integer division, which truncates off the decimal part.

Here is a relevant question: "Java Integer Division, How do you produce a double ?"

如果您确实需要将结果设为int ,则可以在除法之前进行乘法运算,以避免整数除法为零。

int percentagefill = (percent*count)/10;

Change this line:

int percentagefill = (percent/10)*count; 

To:

double percentagefill = (percent/10.0)*count;

This will use floating point arithmetic (because of the 10.0 instead of 10), and store the result in a double (which has precision past the decimal point unlike an int ).

As others have mentioned, you're losing precision with integer division

The solution depends on your needs: if your result needs to be an integer anyway, multiply first:

int percentagefill = (percent*count)/10;

Could be "good enough" for you (you'll get the correct answer rounded down).

If you need to be able to get fractional answers, you need to convert things to floating point types:

double percentagefill = (percent/10.0)*count;
//                                 ^ the .0 makes this a double,
//                                forcing the division to be a
//                                floating-point operation.

It's easy to fix:

  int percent = 2;
  int count = 10;
  double percentagefill = (percent/10.0)*count; 
  System.out.println(percentagefill);

Dave Newton, your answer should have been an answer. :)

The integer 2 / the integer 10 = 0. The integer 0 * the integer 10 = 0.

You will need a float or double data type . While working with information, always be weary of chances for the interpreter to make data type assumptions and "casts". println takes an intefer and casts to a string for display is one example.

Most of my work is in php and when working with values, 0, NULL, ERROR can all be different things and can yield unexpected results. Sometimes you may need to explicitly cast a variable to a different data type to get the intended results.

This is so due to the fact that you are using the integer data type when you should be using a floating-point data type such as double. This code should result in 2.0:

double percent = 2;
double count = 10;
double percentagefill = (percent/10)*count; 
System.out.println(percentagefill);

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