zoukankan      html  css  js  c++  java
  • 四舍五入至某小数位后返回数字串

     /**
       將數字四捨五入至某小數位, 並返回指定位數字串
       function2(123.455,2) => 123.46
       function2(123.449,2) => 123.45
       function2(123.44,3) => 123.440 
       function2(123.4455,3) => 123.446
       function2(123.4499,3) => 123.450
       function2(123.9,0) => 124
       程式接口:
       public static double function2(double value,int decimalPlaces); */
     

       public static double function2(double value,int decimalPlaces){
           double dbl = value*(Math.pow(10, decimalPlaces+1) );
           Double d = new Double(dbl);
           int intValue = d.intValue();
           intValue+=5;  //四舍五入关键步骤
           intValue = intValue/10; //舍弃最后一位
           int value1 = intValue /((int)Math.pow(10, decimalPlaces));
           int value2 = intValue % ((int)Math.pow(10, decimalPlaces));
           String str = value1+"."+value2;
           if(decimalPlaces==0)str = value1+"";
           System.out.println("str = "+str);
           return Double.parseDouble(str);
       } 
        
                                            ---------------------------纵横软件邮件所发笔试题2
  • 相关阅读:
    147
    UVA12230 过河 Crossing Rivers
    重聚
    网络流24题 P2762 太空飞行计划问题
    网络流24题 P2756 飞行员配对方案问题
    网络流24题
    洛谷 P3369 【模板】普通平衡树
    LOJ #6280. 数列分块入门 4
    LOJ #6279. 数列分块入门 3
    LOJ #6278. 数列分块入门 2
  • 原文地址:https://www.cnblogs.com/chaohi/p/2330354.html
Copyright © 2011-2022 走看看