*
float a = 123.2334f;
float b = (float)(Math.round(a*1000))/1000;
System.out.println(b);//123.23
(这里的100就是2位小数点,如果要其它位,如4位,这里两个100改成10000)
*
float f = 34.237323f;
BigDecimal b2 = new BigDecimal(f);
float f1 = b2.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
System.out.println(f1);//34.23
----------------------------------------------------------------
参考BigDecimal.setScale()方法、RoundingMode
float ft = 134.3435f;
int scale = 2;//设置位数
int roundingMode = 4;//表示四舍五入,可以选择其他舍值方式,例如去尾,等等.
BigDecimal bd = new BigDecimal((double)ft);
bd = bd.setScale(scale,roundingMode);
ft = bd.floatValue();
System.out.println(ft);//123.34
*
float scale1= (float) 34.236323;
DecimalFormat fnum = new DecimalFormat( "##0.00 ");
String dd=fnum.format(scale1);
System.out.println(dd);//34.23
*
double d = 3.1415926;
String result = String.format("%.2f",d);
System.out.println(result);//3.14
*
-----未学未用到-------
此外如果使用struts标签做输出的话,有个format属性,设置为format="0.00"就是保留两位小数
例如:<bean:write name="entity" property="dkhAFSumPl" format="0.00" />