Double value to round up in Java

后端 未结 10 1907
我寻月下人不归
我寻月下人不归 2020-12-07 12:44

I have a double value = 1.068879335 i want to round it up with only two decimal values like 1.07.

I tried like this

DecimalFormat df=new         


        
10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 13:20

    You can use format like here,

      public static double getDoubleValue(String value,int digit){
        if(value==null){
            value="0";
         }
        double i=0;
         try {
             DecimalFormat digitformat = new DecimalFormat("#.##");
             digitformat.setMaximumFractionDigits(digit);
            return Double.valueOf(digitformat.format(Double.parseDouble(value)));
    
        } catch (NumberFormatException numberFormatExp) {
            return i;   
        }
    }
    

提交回复
热议问题