如何阻止java四舍五入

时间:2016-10-11 00:24:19

标签: java casting double rounding area

我正在为我的班级做代码,需要输出圆形区域和圆周的面积,周长和位数。输出不应该向上或向下舍入,而只是计算所有出路,但是我的代码没有正确地执行此操作。这是我的代码:

    circumference = 2 * pi * radius;
    area = pi * Math.pow(radius, 2);
    digitString1 = Double.toString(circumference);
    digitString2 = Double.toString(area);

    int count1 = 0;
    for (int i = 0, len = digitString1.length(); i < len; i++) {
        if (Character.isDigit(digitString1.charAt(i))) {
            count1++;
        }
    }

    int count2 = 0;
    for (int i = 0, len = digitString2.length(); i < len; i++) {
        if (Character.isDigit(digitString2.charAt(i))) {
            count2++;
        }
    }

    System.out.println("The Circumference is: " + circumference);
    System.out.println("The Area is: " + area);
    System.out.println("Total number of digits in the circumference is: " + count1);
    System.out.println("Total number of digits in the area is: " + count2);
}

例如,当我输入11时,我得到:

    Circles selected. Please enter the radius: 11
    The Circumference is: 69.11503837897544
    The Area is: 380.132711084365
    Total number of digits in the circumference is: 16
    Total number of digits in the area is: 15

而不是正确的输出:

    Circles selected. Please enter the radius: 11
    The circumference is: 69.11503837897544
    The area is: 380.1327110843649
    Total number of digits in the circumference is: 16
    Total number of digits in the area is: 16

看看该区域末端的649如何向上舍入到... 65。有人可以帮忙吗?谢谢!

0 个答案:

没有答案