-2

I have an app where i want to implement a chart , i have three values total , death cases and the recovered ones , so the numbers are as follow :

  • Total : 14890035
  • Death cases : 614124
  • Recovered cases : 8943850

what i basically want to do is to calculate the percentage of deaths cases and recovered ones off the total so my chart will only have two percentages ( death and recovered )

  • This is what i tried so far but it is not giving me accurate percentages
val main_deaths = intDaaths.times(100).div(intCases).toFloat()
 val main_recovered = intRecovered.times(100).div(intCases).toFloat()

my idea was to multiply the death and recovered numbers to 100 and then divide by total number , but i don't know why it is not giving accurate values ( percentages )

any help is appreciated guys , thank you

Taki
  • 3,290
  • 1
  • 16
  • 41
  • 1
    You have to convert to float before division – Pawel Jul 21 '20 at 13:38
  • You did not tell us the types of the variables, but I guess you are doing an integer division. So the result will be an integer truncated towards 0. – Henry Jul 21 '20 at 13:38
  • You're doing integer division, convert either of the value to Double/Float before doing the division. – Animesh Sahu Jul 21 '20 at 13:39
  • thank you guys for the answers but it still shows missing some of the percentage https://imgur.com/a/wbZQ02q – Taki Jul 21 '20 at 13:56
  • message to those who closed my topic and downvoted my answer , it is actually discouraging approach of dealing with people topics , when what you suggested didnt solve my issue , why would you close my topic and ask me to open new one again , weird – Taki Jul 21 '20 at 13:58
  • I had the same problem for my question: https://stackoverflow.com/q/61299346/12478830 – MMG Jul 21 '20 at 14:04
  • They didn't give me correct answer, but they closed my question. – MMG Jul 21 '20 at 14:05
  • that's totally weird approach , they close people topics before even giving any chance for other people to give suggestions or other solutions and they associate their own answers like if they really solve the issue , frustrating and discouraging approach honesly , i was going to check your topic , they deleted it – Taki Jul 21 '20 at 14:06
  • i didn't close it myself , they closed it – Taki Jul 21 '20 at 14:20
  • `It is actually discouraging approach of dealing with people topics`, sorry to say but your question points to exactly the same problem as the one which we've marked. The problem was integer division and we've linked the question which already has answer for that. **You can always edit your question and add more specific problem to suggest for reopening of the question because you weren't satisfied with the close reason**. – Animesh Sahu Jul 21 '20 at 15:19
  • well it is closed and i voted up an answer , thankx – Taki Jul 21 '20 at 15:42

2 Answers2

0

You're performing integer division. If you convert to float before dividing, that should give you the right answer.

val main_deaths = intDaaths.times(100).toFloat().div(intCases)
val main_recovered = intRecovered.times(100).toFloat().div(intCases)
Marcel
  • 958
  • 1
  • 7
  • 18
  • i tried your approach too by convertiong values to float before devision but it still dosn't give accurate percentage , take look at the screenshot https://imgur.com/a/wbZQ02q – Taki Jul 21 '20 at 13:56
0
val main_deaths = ((intDaaths*100).toFloat()/(intCases).toFloat())
 val main_recovered = ((intRecovered*100).toFloat()/(intCases).toFloat())
MMG
  • 3,226
  • 5
  • 16
  • 43
  • didn't work yet and it is weird after apply your code it still misses that 4 percent like the screenshots shows https://imgur.com/a/wbZQ02q , it is always missing 4 percent even if the numbers are different – Taki Jul 21 '20 at 13:55
  • This is because your data doesn't cover all cases. Not everyone has either recovered or is still infected. The death cases make up for the rest of your chart. – Marcel Jul 21 '20 at 14:00
  • i guess you are correct because when i add both numbers they dont exactly give the same total , thank you mate – Taki Jul 21 '20 at 14:15