Retain precision with double in Java

前端 未结 22 3131
情歌与酒
情歌与酒 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:52

    Why not use the round() method from Math class?

    // The number of 0s determines how many digits you want after the floating point
    // (here one digit)
    total = (double)Math.round(total * 10) / 10;
    System.out.println(total); // prints 11.4
    

提交回复
热议问题