public class Demo03 {
public static void main(String[] args) { //客户端
System.out.println(round2(getArea(13)));
System.out.println(round(3.1415926,3));
System.out.println(round(3.7485926f,4));
}
//求正三角形外接圆面积
public static double getArea(int m){
//已知正三角形外接圆面积公式:PI*m*m/3
double s=Math.PI*m*m/(3*1.0);
return s;
}
//对一个double类型进行四舍五入保留两位小数的操作
public static double round2(double d){
return (Math.round(d*100))/100.0; //系统提供的round方法
}
public static double round(double d,int n){
//"1"+n0
String str="1";
while(n--!=0){
str+="0";
}
int x=Integer.parseInt(str);
return (Math.round(d*x))/(x*1.0);
}
public static double round(float d,int n){
String str="1";
while(n--!=0){
str+="0";
}
int x=Integer.parseInt(str);
return (Math.round(d*x))/(x*1.0);
}
}