gpt4 book ai didi

java - 如何获得小数点后 4 位的输出答案

转载 作者:行者123 更新时间:2023-12-01 14:07:57 25 4
gpt4 key购买 nike

我需要将输出四舍五入到小数点后 4 位,但我不太确定如何让我的东西完全按照我想要的方式运行,只需要将最后一位小数四舍五入到小数点后 4 位

import java.util.Scanner;  //Needed for the Scanner class  

public class SphereCalculations
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);

double radius;
double volume;
double surfaceArea;

System.out.println("Welcome to the Sphere Calculator. ");
System.out.print( "Enter radius of sphere: " );
radius = input.nextDouble();


volume = ((4.0 / 3.0) * (Math.PI * Math.pow(radius, 3)));
surfaceArea = 4 * (Math.PI * Math.pow(radius, 2));


System.out.println("The Results are: ");
System.out.println("Radius: " + radius);
System.out.println("Sphere volume is: " + volume);
System.out.println("Sphere Surface Area is: " + surfaceArea);
}
}

输出:

Radius: 7.5
Volume: 1767.1459
Surface Area: 706.8583

最佳答案

System.out.printf("Sphere Surface Area is: %.4f%n", surfaceArea);

关于java - 如何获得小数点后 4 位的输出答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18752413/

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