分类: Java
2012-11-05 09:31:56
1 | java.text.DecimalFormat df = new java.text.DecimalFormat("#0.##"); |
2 | double d=3.14159; |
3 | System.out.println(df.format(d)); |
Ⅱ
1 | java.math.BigDecimal bd = new java.math.BigDecimal("3.14159265"); |
2 | bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP); |
Ⅲ
01 | class Test{ |
02 | public static void main(String se77en[]){ |
03 | double ret = convert(3.14159); |
04 | System.out.println(ret); |
05 | } |
06 |
07 | static double convert(double value){ |
08 | long l1 = Math.round(value*100); //四舍五入 |
09 | double ret = l1/100.0; //注意:使用100.0而不是100 |
10 | return ret; |
11 | } |
12 | } |
Ⅳ
1 | double d = 3.14159; |
2 | d = (double)((int)(d*100+0.5))/100; |