gpt4 book ai didi

java - 四舍五入并显示值

转载 作者:行者123 更新时间:2023-12-02 08:34:03 24 4
gpt4 key购买 nike

我有以下代码:

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)身份证号;

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

1732

033

6034

3835

38

总和是153.0

归一化值为0

归一化值为0

归一化值为0

归一化值为0

归一化值为0

但是,标准化后,我想要得到 17/153=0.111。我得到的标准化值为 0。我希望对这些值进行四舍五入。

最佳答案

int e[];

...

e[p] = bd1.intValue();

e 是一个整数数组。您如何期望整数值为 0.111?

将 e 更改为某种可以保存小数的类型。

关于java - 四舍五入并显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com