2013-09-25 30 views
0

你好我正在做一個計算頻率分佈的程序,我在獲取類的數量時遇到了問題,因爲我打算在頻率分佈中使用它......類的結果是...

6.286797971382275,這是正確的,但...

我想這一輪關閉以7 ...

java中取整結果

我應該如何去做到這一點?謝謝

String []values = (inputValues.getText().toString().split(",")); 
      int[] convertedValues = new int[values.length]; 
      txtTotalNum.setText(Integer.toString(values.length)); 


      //calculate for the minimum and maximum number 
      Arrays.sort(convertedValues); 

      int max=convertedValues[0]; 
      for(int i=0;i<convertedValues.length;i++){ 
       convertedValues[i] =Integer.parseInt(values[i]); 
       if(convertedValues[i]>max){ 
        max=convertedValues[i]; 
       } 
      } 

      int min = convertedValues[0]; 
      double classes=0; 
      for(int i=0;i<convertedValues.length;i++){ 
       convertedValues[i] =Integer.parseInt(values[i]); 
       if(convertedValues[i]<min){ 
        min=convertedValues[i]; 

       } 
      } 

      txtMinimum.setText(Integer.toString(min)); 
      txtMaximum.setText(Integer.toString(max)); 


      //calculate for the range 

      int range=max - min; 
      txtRange.setText(Integer.toString(range)); 

      //calculate for the # of classes 

       classes=1+3.3*Math.log10(convertedValues.length); 

       Classes.setText(Double.toString(classes)); 
+0

你爲什麼不使用類似'Math.ceil()'? –

+0

你嘗試'Math.ceil()'嗎? – ahmedalkaff

回答

2

考慮數學課。

Math.ceil()

2

只需使用Math.ceil()。它將round up你的號碼到最接近的整個值。請注意,它仍然會返回一個double。

3

使用Math.ceil()

Math.ceil(6.286797971382275); 

這就是它要回報你,

最小的(最接近負無窮大)浮點值,它 大於或等於參數並等於一個 數學整數。

在使用之前閱讀API。

+1

「在使用之前閱讀API」,哈!我們希望,我們可以希望 –

0

使用下面的代碼。 double dval = 6.286797971382275; System.out.println(dval); System.out.println(Math.ceil((dval)));

0

您可以使用DecimalFormat旁邊Math.ceil()

double input = 6.286797971382275; 
DecimalFormat df = new DecimalFormat("#"); 

df.setRoundingMode(RoundingMode.UP); 
String output = df.format(input); 

System.out.println("output : " + output);