在 Java 中舍入? [英] Rounding in Java?

查看:37
本文介绍了在 Java 中舍入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的作业如下:

将 20 对数字(分别为 ID 号和分数)读入两个单独的数组.求平均分.打印一张如下表所示的每个学生的 ID、分数和差异(分数 - 平均值),每行一个学生.如图所示,在表头打印总分、平均值和分数.将平均值和差值四舍五入到小数点后两位.

Read 20 pairs of numbers (ID number and score respectively) into two separate arrays. Find the average score. Print a table as shown below of the ID, score and difference (score - average) for each student, one student per line. Print the sum, average, and count of score at the head of the table as shown. Round the average and difference to 2 decimal places.

我的程序中有一个应该很简单的错误,但由于某种原因我无法弄清楚.我已经想出了如何做整件事,但由于某种原因,我的减法关闭了.这是一个示例输出供参考:

I have a bug in my program that should be really simple, but I just can't figure it out for some reason. I've figured out how to do the whole thing, but for some reason, my subtraction is off. Here's a sample output for reference:

ID  Score   Difference
115 257.0   14.349999999999994
123 253.0   10.349999999999994
116 246.0   3.3499999999999943
113 243.0   0.3499999999999943
112 239.0   -3.6500000000000057
104 239.0   -3.6500000000000057
110 238.0   -4.650000000000006
218 243.0   0.3499999999999943
208 242.0   -0.6500000000000057
222 223.0   -19.650000000000006
223 230.0   -12.650000000000006
213 229.0   -13.650000000000006
207 228.0   -14.650000000000006
203 224.0   -18.650000000000006
305 265.0   22.349999999999994
306 262.0   19.349999999999994
311 256.0   13.349999999999994
325 246.0   3.3499999999999943
321 245.0   2.3499999999999943
323 245.0   2.3499999999999943

问题是当我调用我的程序只打印它计算的平均值时,它是一个很好的四舍五入的 242.65.而且由于 score[k] 显然也是一个整数,我不明白这是怎么发生的?有什么办法可以让您了解这里出了什么问题?

The problem is that when I call my program to just print the average that it's computed, it's a nice rounded 242.65. And since the score[k] is obviously also a round number, I don't understand how this is happening? Any way you can shed some light on what's going wrong here?

import java.util.Scanner;
import java.io.*;

public class prog402a
{
    public static void main(String args[])
    {
        Scanner inFile = null;
        try
        {
            inFile = new Scanner(new File("prog402a.dat"));
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found!");
            System.exit(0);
        }

        int[] idNumber = new int[20];
        double[] score = new double[20];

        for(int k = 0; k < idNumber.length; k++)
        {
            idNumber[k] = inFile.nextInt();
            score[k] = inFile.nextDouble();
        }

        double sum = 0;

        for(int k = 0; k < score.length; k++)
        {
            sum += score[k];
        }

        double doubSum = (double)sum;
        double average = doubSum/20.0;
        average=(int)(average*100+.5)/100.0;
        System.out.println(average);

        System.out.println("ID\tScore\tDifference");
        for(int k = 0; k < idNumber.length; k++)
        {
            System.out.println(idNumber[k] + "\t" + score[k] + "\t" + (score[k] - average));
        }



        inFile.close();

    }
}

推荐答案

浮点使用二进制系统来表示数字.这意味着在十进制系统中可以用有限数量的数字表示的一些数字不能在二进制系统中表示.例如,无法正确表示 0.2 二进制文件.因此,该算法将使用它可以表示的最接近的值.因此,它将适用于 0.199999...-ish 或 0.2000...1-ish.注意十进制系统也有问题,例如表示 1/3 是用 0.333333.... 完成的,所以如果你用有限的数字来计算.你总会犯一个(小)错误.

Floating points use the binary system to represent numbers. That means some numbers that can be represented with a finite amount of digits in the decimal system can't in the binary system. For instance there is no way to correctly represent 0.2 binary. As a result, the algorithm will use the closest value it can represent. Thus it will work with something 0.199999...-ish or 0.2000...1-ish. Note the decimal system has problems too, for instance representing 1/3 is done with 0.333333.... so if you would calculate with a limited number of digits. You will always make a (small) error.

然而,存在一个类 - BigDecimal 能够以任意精度计算分数,尽管我认为计算 20 学生的平均值只会有所不同7 th 位左右.

There exists however a class - BigDecimal to that is able to calculate the score with an arbitrary precision, although I think calculating the average for 20 students will only differ on the 7th digit or so.

因此,我建议简单地格式化数字,例如,它可以精确地显示两位数的结果.你可以这样做:

Therefore I recommend simply to format the numbers such that for instance, it displays the result precise on two digits. You can do this with:

DecimalFormat myFormatter = new DecimalFormat("0.000"); //the pattern
String output = myFormatter.format(number);
System.out.println(output);

这篇关于在 Java 中舍入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆