Retain precision with double in Java

前端 未结 22 3084
情歌与酒
情歌与酒 2020-11-21 05:15
public class doublePrecision {
    public static void main(String[] args) {

        double total = 0;
        total += 5.6;
        total += 5.8;
        System.out         


        
22条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:53

    private void getRound() {
        // this is very simple and interesting 
        double a = 5, b = 3, c;
        c = a / b;
        System.out.println(" round  val is " + c);
    
        //  round  val is  :  1.6666666666666667
        // if you want to only two precision point with double we 
                //  can use formate option in String 
               // which takes 2 parameters one is formte specifier which 
               // shows dicimal places another double value 
        String s = String.format("%.2f", c);
        double val = Double.parseDouble(s);
        System.out.println(" val is :" + val);
        // now out put will be : val is :1.67
    }
    

提交回复
热议问题