如何使double为整数时不带小数点
假设一个double类型的变量被赋值为5,则输出时会显示5.00000
怎样才能在输出时去掉小数点?
并且当它赋值为小数时(比如5.12)输出时会仍显示小数
public class DoubleTrans{
public static String doubleTrans(double d){
if(Math.round(d)-d==0){
return String.valueOf((long)d);
}
return String.valueOf(d);
}
public static void main(String args[]){
double a=3.0d,b=3.1d;
System.out.println(doubleTrans(a));
System.out.println(doubleTrans(b));
}
}
运行结果:3 3.1
public class s {
public static void main(String[] args) {
double b=1.1;
double c=b-(int)b;
if(c==0)
System.out.println((int)b);
else
System.out.println(b);
}
}