http://blog.csdn.net/com_stu_zhang/article/details/7214565
方法1:
float f =34.232323; BigDecimal b= new BigDecimal(f); float f1 =b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue(); // b.setScale(2,BigDecimal.ROUND_HALF_UP) 表明四舍五入,保留两位小数
即
[java]view plaincopy
- floatft=134.3435f;intscale=2;//设置位数introundingMode=4;//表示四舍五入,可以选择其他舍值方式,例如去尾,等等.BigDecimalbd=newBigDecimal((double)ft);bd=bd.setScale(scale,roundingMode);ft=bd.floatValue();
方法2:
import java.text.DecimalFormat;String a =new DecimalFormat("###,###,###.##").format(100.12345 );
即[java]view plaincopy
- floatscale=34.236323;DecimalFormatfnum=newDecimalFormat("##0.00");Stringdd=fnum.format(scale);ystem.out.println(dd);
方法3:最简单的方法
[java]view plaincopy
- floata=123.2334f;floatb=(float)(Math.round(a*100))/100;(这里的100就是2位小数点,如果要其它位,如4位,这里两个100改成10000)
希望你灰暗的心情在此刻明亮起来,去迎接美好的明天!