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 doub
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/19 10:21:35
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 doub
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位/** * 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 doub
DecimalFormat df = new DecimalFormat("0.00");
double d = 123.9078;
double db = df.format(d);
则db=123.90;