四舍五入并显示值
我有以下代码:
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();
}
}
}
我有一个名为 Consumer1 的表。计算总和后,我得到的值如下
mysql>按 Service_ 从 Consumer1 组中选择 Service_ID,sum(consumer_feedback) ID;
Service_ID总和(consumer_feedback) 31 17 32 0 33 60 34 38 35 | 35 38 在我的程序中,我正确地获得了每个 Service_ID 的总和。但是,在标准化之后,即当我计算 17/153=0.111 时,我得到的标准化值是 0。我希望在四舍五入后正确显示标准化值。我输出如下
C:>javac Testd1.java
C:>java Testd1 具体列的总和! 31
17 32
0 33
60 34
38 35
38
总和为153.0
归一化值为0
归一化值为0
归一
化值为0 归一化
值为0 归一化值为0
但是,归一化后,我想得到17/153=0.111。我得到的标准化值为 0。我希望这些值被四舍五入。
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
mysql> select Service_ID,sum(consumer_feedback) from consumer1 group by Service_
ID;
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
C:>javac Testd1.java
C:>java Testd1
Sum of the specific column!
31
17
32
0
33
60
34
38
35
38
The sum is153.0
The normalized value is0
The normalized value is0
The normalized value is0
The normalized value is0
The normalized value is0
But, after normalization, I want to get 17/153=0.111. I am getting the normalized value is 0. I want these values to be rounded off.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
...
e 是一个整数数组。您如何期望一个整数的值为 0.111?
将 e 更改为某种可以保存小数的类型。
...
e is an array of integers. How do you expect the value of an integer to 0.111?
Change e to some type that can hold decimal fractions.
其他一些评论:
我认为您想将以下内容设置为
0
,因为您的e
数组似乎是从零开始的。Java 中的局部变量不会初始化为任何默认值,因此在定义变量时初始化变量是一个很好的做法:
意识到您已经硬编码了
e
数组的大小,但是您的查询可能返回更多的行,并且您的程序将崩溃。以下代码:
...不会执行您想要的操作。这是您正在做的事情的另一个视图:
您将结果存储在整数变量中,因此在到达以下行之前您已经丢失了结果的小数部分:
Some other comments:
I think you want to set the following to
0
, since youre
array seems to be zero based.Local variables in Java don't get initialized to any default value, so it's good practice to initialize variables when you define them:
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.The following code:
...won't do what you want. Here's another view of what you're doing:
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: