作业帮 > 综合 > 作业

java 保留小数点后2位

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/04/29 23:16:01
java 保留小数点后2位
/**
* Write a description of class Cylinder here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cylinder
{
// instance variables - replace the example below with your own
private double radius;
private double height;

/**
* Constructor for objects of class Cylinder
*/
public Cylinder(double r, double h)
{
// initialise instance variables
radius = r;
height = h;

double newBase = base();
System.out.println(" the value of the base is: " + newBase);
double newCirc = circumference();
System.out.println(" the value of the Circ is: " + newCirc);
}
public double base()
{
double answer= Math.PI * radius * radius;

return answer;
}
public double circumference()
{
double answer=Math.PI * radius;
return answer;
}
}
保留2位小数
java 保留小数点后2位
DecimalFormat df = new DecimalFormat("0.00");
double d = 123.9078;
double db = df.format(d);
则db=123.90;