强制转换
double保留小数点后一位
Decima2.double转int
int num = ( new Double(0.999997)).intValue();
或
int num = (int)0.999997;
使用DecimalFormat
lFormat df = new DecimalFormat( “#.0” );
df.format(0.999997);
同理,保留小数点后两位
DecimalFormat df = new DecimalFormat( “#.00” );
df.format(0.999997);
同理保留小数点后零位
DecimalFormat df = new DecimalFormat( “#” );
df.format(0.999997);
示例
/**
* (2)四舍五入把double转化为int类型整数,0.5也舍去,0.51进一
* @param dou
* @return
*/
public static int DoubleFormatInt(Double dou){
DecimalFormat df = new DecimalFormat(“######0”); //四色五入转换成整数
return Integer.parseInt(df.format(dou));
}