gpt4 book ai didi

java - 如何在Java中更改2.516415E7号码?

转载 作者:行者123 更新时间:2023-12-01 10:12:58 24 4
gpt4 key购买 nike

嘿,我在数字表示方面遇到问题。代码工作正常,但结果是 2.516415E7 数字,结果很好,但我更喜欢像 25164150 这样的正常表示形式。不幸的是,我不能使用长类型,因为我在代码中使用了 Math.pow(仅适用于 double 类型)有可能吗?我怎样才能用Java做到这一点?

这是我的代码:

//Find the difference between the sum of the squares of the first one hundred natural numbers
//and the square of the sum.

public class Sum_square_difference {
public double Sum_of_the_squares() {
double sum = 0;

for (int i = 1; i <= 100; i++) {
double squares = 0;

squares = Math.pow(i, 2);
sum += squares;
}
return sum;
}

public double Sum_of_the_natural_numbers() {
double result = 0;
double sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
result = Math.pow(sum, 2);
return result;
}

public static void main(String[] args) {
Sum_square_difference sqare = new Sum_square_difference();
System.out.println(sqare.Sum_of_the_squares());
System.out.println(sqare.Sum_of_the_natural_numbers());
System.out.println(sqare.Sum_of_the_natural_numbers() - sqare.Sum_of_the_squares());
}
}

最佳答案

一种可能的选择是使用格式化的 io.比如,

double val = 2.516415E7;
System.out.printf("%.0f%n", val);

输出是(根据要求)

25164150

关于java - 如何在Java中更改2.516415E7号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36046933/

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