取小数点前两位,并四舍五入

  1. ava 取小数点前两位的4种办法  
  2. //取小数点的几种办法  
  3. class TestDot2   
  4.  
  5. //方法1  
  6. public void test1(double c)  
  7.  
  8.    java.text.DecimalFormat df new java.text.DecimalFormat("#.##");  
  9.    System.out.println(df.format(c));  
  10.  
  11. //方法2  
  12. public void test2(double c)  
  13.  
  14.      
  15.    java.math.BigDecimal bd     new   java.math.BigDecimal(String.valueOf(c));     
  16.    bd     bd.setScale(2,java.math.BigDecimal.ROUND_HALF_UP);     
  17.    System.out.println(bd);  
  18.  
  19. //方法3  
  20. public void test3(double c)  
  21.  
  22.    long   l1     Math.round(c*100);   //四舍五入     
  23.         double   ret     l1/100.0              //注意:使用   100.0   而不是   100       
  24.   
  25.    System.out.println(ret);  
  26.  
  27. //方法4  
  28. public void test4(double c)  
  29.   
  30.    c=((int)(c*100))/100.0  
  31.    System.out.println(c);  
  32.  
  33. public static void main(String[] args)   
  34.  
  35.    double 3.056 
  36.    TestDot2 td2 new TestDot2();  
  37.    //td2.test1(c);  
  38.    //td2.test2(c);  
  39.    //td2.test3(c);  
  40.    td2.test4(c);  
  41.  
  42. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ma_xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值