知识点补充:
关于double类型小数点保留几位问题:
1.String.format
System.out.println(String.format("%.5f", sum1));
2.System.out.printf("%.5f",sum1);//float 或者double
3.DecimalFormat
DecimalFormat df=new DecimalFormat("#.00000");
System.out.println(df.format(sum1));
4BigDecimal
import java.text.DecimalFormat;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int n;
n = input.nextInt();
int i;
double sum = 0;
for (i = 1; i <= n; i++) {
sum += 1.0 / (4 * i - 3) - 1.0 / (4 * i - 1);
}
double sum1 = sum * 4.0;
System.out.println(String.format("%.5f", sum1));// 保留5位小数
// 2. System.out.printf("%.5f",sum1);//float 或者double
// 3. DecimalFormat df=new DecimalFormat("#.00000");
// System.out.println(df.format(sum1));
input.close();
}
}