Retain precision with double in Java

前端 未结 22 3136
情歌与酒
情歌与酒 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 06:02

            /*
            0.8                     1.2
            0.7                     1.3
            0.7000000000000002      2.3
            0.7999999999999998      4.2
            */
            double adjust = fToInt + 1.0 - orgV;
            
            // The following two lines works for me. 
            String s = String.format("%.2f", adjust);
            double val = Double.parseDouble(s);
    
            System.out.println(val); // output: 0.8, 0.7, 0.7, 0.8
    

提交回复
热议问题