试用setScale方法可以达到这个目的,如下:double f = 10.66666667; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();则f1即为所求 b.setScale(2, BigDecimal.ROUND_HALF_UP) 表明四舍五入,保留两位小数 上面是对double类型的处理,如果对float的,则将上面所有的double字段换成float就行
我给人下方法给你: import java.text.DecimalFormat; public String roundUp(double d, String RoundFormat) { DecimalFormat nf = new DecimalFormat(RoundFormat); return nf.format(d); } parseDouble(String s) 如果是两位小数: double f = 10.66666667; f=Double.parseDouble(roundUp(f,"0.00")) f-->10.67
我给人下方法给你: import java.text.DecimalFormat; public static String roundUp(double d, String RoundFormat) { DecimalFormat nf = new DecimalFormat(RoundFormat); return nf.format(d); } parseDouble(String s) 如果是两位小数: double f = 10.66666667; f=Double.parseDouble(roundUp(f,"0.00")); f-->10.67
最簡單double f = 10.6666666667; f = Math.round(f*100)/100;
少了個小數點 最簡單double f = 10.6666666667; f = Math.round(f*100)/100.0;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();则f1即为所求
b.setScale(2, BigDecimal.ROUND_HALF_UP) 表明四舍五入,保留两位小数
上面是对double类型的处理,如果对float的,则将上面所有的double字段换成float就行
import java.text.DecimalFormat;
public String roundUp(double d, String RoundFormat)
{
DecimalFormat nf = new DecimalFormat(RoundFormat);
return nf.format(d);
}
parseDouble(String s)
如果是两位小数:
double f = 10.66666667;
f=Double.parseDouble(roundUp(f,"0.00"))
f-->10.67
import java.text.DecimalFormat;
public static String roundUp(double d, String RoundFormat)
{
DecimalFormat nf = new DecimalFormat(RoundFormat);
return nf.format(d);
}
parseDouble(String s)
如果是两位小数:
double f = 10.66666667;
f=Double.parseDouble(roundUp(f,"0.00"));
f-->10.67
f = Math.round(f*100)/100;
最簡單double f = 10.6666666667;
f = Math.round(f*100)/100.0;