Java Android分区的结果总是0
请检查下面的android代码Java Android分区的结果总是0,java,android,math,double,integer-division,Java,Android,Math,Double,Integer Division,请检查下面的android代码 private void preMealChartCreator(int numberOfPreMealLow, int numberOfPreMealNormal, int numberOfPreMealHigh, int totalReadings) { Log.d("OverViewFragment","chart_low: "+numberOfPreMealLow+" chart_normalssss: "+numberOfPreMeal
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));
}
无论我做什么,我的百分比总是0。我想检查是否有任何内容是null
或0,因此我打印了日志,一切正常。日志在下面
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
我甚至试着手动转换为双精度,然后硬编码值,但都不起作用。这里出了什么问题?在除法之前将其中一个变量强制转换为浮动:
int preMealLowPercentage=(int)((numberOfPreMealLow/(float)总读数)*100)代码>
因为否则你是在用整数运算
或者您可以简单地移动*100
,如:
int preMealLowPercentage=(int)((numberOfPreMealLow*100/总读数))代码>在除法之前将其中一个变量强制转换为浮动:
int preMealLowPercentage=(int)((numberOfPreMealLow/(float)总读数)*100)代码>
因为否则你是在用整数运算
或者您可以简单地移动*100
,如:
int preMealLowPercentage=(int)((numberOfPreMealLow*100/总读数))代码>