简体   繁体   English

四舍五入并显示值

[英]round off and displaying the values

I have the following code: 我有以下代码:

import java.sql.*;
import java.math.*;

public class Testd1 {

    public static void main(String[] args) {

        System.out.println("Sum of the specific column!");

        Connection con = null;
        int m = 1;
        double sum, sum1, sum2;
        int e[];
        e = new int[100];
        int p;

        int decimalPlaces = 5;

        for (int i = 0; i < e.length; i++) {
            e[i] = 0;
        }

        double b2, c2, d2, u2, v2;
        int i, j, k, x, y;
        double mat[][] = new double[10][10];

        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/prathi", "root", "mysql");
            try {
                Statement st = con.createStatement();

                ResultSet res = st.executeQuery(
                    "SELECT  Service_ID,SUM(consumer_feedback) " +
                    "FROM  consumer1 group by Service_ID");
                while (res.next()) {
                    int data = res.getInt(1);
                    System.out.println(data);
                    System.out.println("\n\n");

                    int c1 = res.getInt(2);
                    e[m] = res.getInt(2);
                    if (e[m] < 0) {
                        e[m] = 0;
                    }
                    m++;
                    System.out.print(c1);
                    System.out.println("\t\t");
                }
                sum = e[1] + e[2] + e[3] + e[4] + e[5];
                System.out.println("\n \n The sum is" + sum);
                for (p = 21; p <= 25; p++) {
                    if (e[p] != 0) {
                        e[p] = e[p] / (int) sum;
                        //I have type casted sum to get output
                    }
                    BigDecimal bd1 = new BigDecimal(e[p]);
                    bd1 = bd1.setScale(decimalPlaces,
                        BigDecimal.ROUND_HALF_UP); // setScale is immutable
                    e[p] = bd1.intValue();
                    System.out.println("\n\n The normalized value is" + e[p]);

                    mat[4][p - 21] = e[p];
                }

            } catch (SQLException s) {
                System.out.println("SQL statement is not executed!");
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

I have a table named consumer1.After calculating the sum i am getting the values as follows 我有一个名为Consumer1的表。计算总和后,我得到的值如下

mysql> select Service_ID,sum(consumer_feedback) from consumer1 group by Service_ ID; mysql>从user1组中选择Service_ID,sum(consumer_feedback);

Service_ID sum(consumer_feedback) 31 17 32 0 33 60 34 38 35 | Service_ID sum(consumer_feedback)31 17 32 0 33 60 34 38 35 | 38 In my program I am getting the sum for each Service_ID correctly.But,after normalization ie while I am calculating 17/153=0.111 I am getting the normalized value is 0.I want the normalized values to be displayed correctly after rounding off.My output is as follows 38在我的程序中,我正确地获得了每个Service_ID的总和。但是,在归一化之后,即当我计算17/153 = 0.111时,我得到的归一化值为0。我希望在四舍五入后正确显示归一化的值。我的输出如下

C:>javac Testd1.java C:> javac Testd1.java

C:>java Testd1 Sum of the specific column! C:> java Testd1特定列的总和! 31 31

17 32 17 32

0 33 0 33

60 34 60 34

38 35 38 35

38 38

The sum is153.0 总和是153.0

The normalized value is0 归一化值为0

The normalized value is0 归一化值为0

The normalized value is0 归一化值为0

The normalized value is0 归一化值为0

The normalized value is0 归一化值为0

But, after normalization, I want to get 17/153=0.111. 但是,归一化后,我想得到17/153 = 0.111。 I am getting the normalized value is 0. I want these values to be rounded off. 我得到的归一化值为0。我希望将这些值四舍五入。

int e[];

... ...

e[p] = bd1.intValue();

e is an array of integers. e是一个整数数组。 How do you expect the value of an integer to 0.111? 您如何期望整数值为0.111?

Change e to some type that can hold decimal fractions. 将e更改为可以容纳小数的某种类型。

Some other comments: 其他一些评论:

I think you want to set the following to 0 , since your e array seems to be zero based. 我认为您想将以下内容设置为0 ,因为您的e数组似乎是从零开始的。

int m = 1;

Local variables in Java don't get initialized to any default value, so it's good practice to initialize variables when you define them: Java中的局部变量不会初始化为任何默认值,因此在定义变量时最好初始化它们:

double sum = 0.0, sum1 = 0.0, sum2 = 0.0;

Realize that you've hard-coded the size of your e array, but your query could return a greater number of rows, and your program would crash. 意识到您已经硬编码了e数组的大小,但是查询可能返回更多的行,并且程序将崩溃。

int e[] = new int[100];

The following code: 如下代码:

e[p] = e[p] / (int) sum;

...won't do what you want. ...不会做你想要的。 Here's another view of what you're doing: 这是您在做什么的另一种观点:

int = int / (int) double;

You're storing the result in an integer variable, so you've already lost the fractional portion of your result before you even get to the following line: 您将结果存储在一个整数变量中,因此在进入以下行之前,您已经丢失了结果的小数部分:

BigDecimal bd1 = new BigDecimal(e[p]);

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

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