Reputation: 23035
Please check the below android code
private void preMealChartCreator(int numberOfPreMealLow, int numberOfPreMealNormal, int numberOfPreMealHigh, int totalReadings) {
Log.d("OverViewFragment","chart_low: "+numberOfPreMealLow+" chart_normalssss: "+numberOfPreMealNormal+" chart_high: "+numberOfPreMealHigh+" chart_total: "+totalReadings);
int preMealLowPercentage = (int)((numberOfPreMealLow/totalReadings)*100);
double preMealNormalPercentage = (double)((numberOfPreMealNormal/totalReadings)*100);
int preMealHighPercentage = (int)((numberOfPreMealHigh/totalReadings)*100);
Log.d("OverViewFragment","chart_low: "+"low_percentage: "+preMealLowPercentage+" normal_percentage: "+preMealNormalPercentage+ "high_percentage: "+preMealHighPercentage);
Log.d("OverViewFragment","chart_low: "+ " chart_normalssss: "+numberOfPreMealNormal+ " chart_total: " +totalReadings+" normal_manual: "+ (1/5)*100);
lowBarView.set(Color.GRAY, preMealLowPercentage);
normalBarView.set(Color.GREEN, (int)preMealNormalPercentage);
highBarView.set(Color.YELLOW, preMealHighPercentage);
verHighBarView.set(Color.RED, 6);
lowTextView.setText(getPercentage(preMealLowPercentage));
normalTextView.setText(getPercentage((int)preMealNormalPercentage));
highTextView.setText(getPercentage(preMealHighPercentage));
veryHighTextView.setText(getPercentage(6));
}
No matter what I do, my percentrages are always 0. I wanted to check whether anything is null
or 0, so I printed the log, everything fine. Log is below
01-13 18:50:08.647 9232-9232/xx.com.au.xxD/OverViewFragment: chart_low: 0 chart_normalssss: 1 chart_high: 2 chart_total: 5
01-13 18:50:08.647 9232-9232/xx.com.au.xxD/OverViewFragment: chart_low: low_percentage: 0 normal_percentage: 0.0high_percentage: 0
01-13 18:50:08.647 9232-9232/xx.com.au.xxD/OverViewFragment: chart_low: chart_normalssss: 1 chart_total: 5 normal_manual: 0
I even tried manually converting to double
and then hard coding values, nothi8ng worked. What is wrong here?
Upvotes: 0
Views: 57
Reputation: 648
Cast one of variables to float
before division:
int preMealLowPercentage = (int)((numberOfPreMealLow/(float)totalReadings)*100);
Because otherwise you're operating with integers.
Or you could simply move *100
like:
int preMealLowPercentage = (int)((numberOfPreMealLow*100/totalReadings));
Upvotes: 2