0

In my Java code, I tried to use Math.round() method. However, when the number is 204.6, method rounds it to 204. I couldn't solve this problem. Here is my code;

//all[i] value of the index zero is 186.
//and it prints 204 not 205.
double updatedPassed = all[i].getPassed()+((all[i].getPassed()*10)/100);
System.out.println(Math.round(updatedPassed));
Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
  • 1
    I can definitely confirm that Math.round(204.6D) rounds to 205. Cast the first number of your updatedPassed variable to be a double before doing your multiplication and division. – Jacob Oct 20 '16 at 19:11
  • There is an answer already here http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Lokesh Pandey Oct 20 '16 at 19:11
  • what is the return type of `getPassed()` method? Seems like it's not a floating point number and the problem is not in `Math.round()` method but in the evaluating of `updatedPassed` variable. Try to replace 100 with 100.0 – Dmitry Smorzhok Oct 20 '16 at 19:12
  • Jacob alluded to this already, but just to emphasize: did you try calling `System.out.println(Math.round(204.6));`? It works fine. The problem lies elsewhere. When trying to solve a programming problem, always try to reduce the problem to its smallest form ([mcve]). – DavidS Oct 20 '16 at 19:14
  • @Lokesh It is not the case that I asked. Thank you. –  Oct 20 '16 at 19:17
  • Hello @Alperen. Stack Overflow is divided into question and answer section. Don't [edit your question to add solution into it](https://stackoverflow.com/revisions/40162611/2). Instead you can post your own separate answer, or if it was already posted you can accept it as your solution. More info at: ["How does accepting an answer work?"](http://meta.stackexchange.com/a/5235). – Pshemo Oct 20 '16 at 19:18

4 Answers4

1

Add a D to 100 to indicate that the division must be done with a floating value. In this way, you will get a floating result. Otherwise, it uses an integer value as result.
And as arithmetically, you get 204.XXX as result, it gives 204 as integer result.

//all[i] value of the index zero is 186.
//and it prints 204 not 205.
double updatedPassed = all[i].getPassed()+((all[i].getPassed()*10)/100D);
System.out.println(Math.round(updatedPassed));
davidxxx
  • 104,693
  • 13
  • 159
  • 179
  • @AlperenÖzdemir The proper way to indicate that an answer solved your problem is to [accept](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it. Doing so will also prevent your question from being [automatically deleted](http://meta.stackexchange.com/questions/78048/enable-automatic-deletion-of-old-unanswered-zero-score-questions-after-a-year/92006#92006) in the future with all of its answers. – dorukayhan Oct 20 '16 at 19:19
0

The code that multiplies all[i] by 10 and dividing by 100 will try and cast it to int. Try doing this:

double updatedPassed = all[i].getPassed()+(((double)all[i].getPassed()*10)/100);
System.out.println(Math.round(updatedPassed));

Hope this helps

Shubhankar S
  • 415
  • 2
  • 8
0

Extracted OP's answer from previous version of question:

I solved the problem with this changes:

double updatedPassed = (double)all[i].getPassed()+((double)(all[i].getPassed()*10)/100);
Community
  • 1
  • 1
Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
-1

If your all[i] array is an array of integers, you'll run into this problem. The array needs to be of type double as integer division results in an integer.

Not sure if its the main issue as you didn't mention what type the array was.

danielku97
  • 60
  • 8