我需要将输出四舍五入到小数点后 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