简体   繁体   English

在Java中四舍五入结果

[英]Rounding off result in java

Hello i am making a program that computes the frequency distribution and i have a problem in getting the number of class because I am going to use this in frequency distribution... the result of # of class is... 您好,我正在编写一个计算频率分布的程序,由于要在频率分布中使用该类,因此在获取类数方面存在问题...类数的结果是...

6.286797971382275 and it is correct but... 6.286797971382275这是正确的,但是...

i want to round this off to 7... 我想四舍五入到...

how am i going to do that? 我该怎么做? thanks 谢谢

    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));

Use Math.ceil() 使用Math.ceil()

Math.ceil(6.286797971382275);

This is what it is going to return you, 这就是要返回你的东西,

The smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer. 大于(等于)自变量且等于数学整数的最小(最接近负无穷大)浮点值。

Read the API before using it. 使用前请阅读API。

Consider the Math class. 考虑数学课。

Math.ceil() Math.ceil()

Simply use Math.ceil() . 只需使用Math.ceil() It will round up your number to the nearest whole value. 它将您的数字四舍五入到最接近的整数。 Note, it still returns a double. 注意,它仍然返回一个双精度值。

Use below code. 使用下面的代码。 double dval = 6.286797971382275 ; 双重dval = 6.286797971382275; System.out.println(dval); System.out.println(dval); System.out.println(Math.ceil((dval))); System.out.println(Math.ceil((dval)));

You can use DecimalFormat beside Math.ceil() 您可以在Math.ceil()旁边使用DecimalFormat

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM