Retain precision with double in Java

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

    Doubles are approximations of the decimal numbers in your Java source. You're seeing the consequence of the mismatch between the double (which is a binary-coded value) and your source (which is decimal-coded).

    Java's producing the closest binary approximation. You can use the java.text.DecimalFormat to display a better-looking decimal value.

提交回复
热议问题