2017-01-13 2 views
0

になり Androidの部門は常に0

アンドロイドのコードの下
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)); 

    } 

を確認してくださいどんなに私は何をすべきか、私のpercentragesは常に0ではない私は何がnullまたは0であるかどうかを確認したかったので、私は、ログを印刷しました、すべての細かい。私も手動doubleに変換し、次にハード値を符号化する試み

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 

下にログがある、nothi8ngが働きました。ここで何が間違っていますか?

答えて

2

は、分割前floatに変数のいずれかをキャスト:

int preMealLowPercentage = (int)((numberOfPreMealLow/(float)totalReadings)*100);

そうでなければ、整数で動作しているため。

それとも、単に同じよう*100を移動することができ:

int preMealLowPercentage = (int)((numberOfPreMealLow*100/totalReadings));