【问题标题】:Why is my formatting so strange?为什么我的格式如此奇怪?
【发布时间】:2018-07-21 10:29:16
【问题描述】:

我的代码格式有问题。最后,它应该打印出结果。我正在使用 printf 语句,但它返回的数字并不像我需要的那样精确。例如,如果一个数字应为 76.83200000000001,则返回为 76.83200。它还在数字末尾添加了不必要的零,如果一个数字应该是 28.0,它就会变成 28.000000。如果可能,我们可以在没有BigDecimal 变量的情况下执行此操作吗?到目前为止,这是我的代码(注意:某些字符串前面有空格,这是因为我提交的网站出于某种原因需要这样做):

import java.util.Scanner;
public class Population {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        double startingNumber, dailyIncrease, daysWillMultiply, temp, population;

        System.out.print("Enter the starting number organisms: ");
        startingNumber = stdin.nextDouble();
        while(startingNumber < 2) {
            System.out.print("Invalid. Must be at least 2. Re-enter: ");
            startingNumber = stdin.nextDouble();
        }

        System.out.print("Enter the daily increase: ");
        dailyIncrease = stdin.nextDouble();
        while(dailyIncrease < 0) {
            System.out.print("Invalid. Enter a non-negative number: ");
            dailyIncrease = stdin.nextDouble();
        }

        System.out.print("Enter the number of days the organisms will multiply: ");
        daysWillMultiply = stdin.nextDouble();
        while(daysWillMultiply < 1) {
            System.out.print("Invalid. Enter 1 or more: ");
            daysWillMultiply = stdin.nextDouble();
        }


        temp = startingNumber * dailyIncrease;
        population = startingNumber;

        System.out.println("Day\t\tOrganisms");
        System.out.println("-----------------------------");
        System.out.println("1\t\t" + startingNumber);
        for (int x = 2; x <= daysWillMultiply; x++) {
            population += temp;
            temp = population * dailyIncrease;
            System.out.printf(x + "\t\t%f\n", population);
        }
    }
}

【问题讨论】:

    标签: java formatting printf number-formatting


    【解决方案1】:

    检查这个帖子的第一个答案,它可能会有所帮助。

    How many significant digits have floats and doubles in java?

    重要的是要了解精度不是统一的,而且 这不是对整数的精确存储。

    【讨论】:

      【解决方案2】:

      好吧,我删除了我之前的答案,因为它绝对是错误的(感谢@JohnKugelman 指出这一点)。我认为由于转换为float 会丢失精度,但事实并非如此。

      根据formatter documentation,使用%f 标志时会发生以下情况:

      • 幅度m(参数的绝对值)被格式化为 m 的整数部分,没有前导零,后跟 小数分隔符后跟一个或多个十进制数字表示 m的小数部分。

      • m 小数部分的结果位数等于精度。 如果未指定精度,则 默认值为 6

      • 如果精度小于将出现的位数 在返回的字符串中的小数点之后 Float.toString(float)Double.toString(double) 分别, 然后该值将使用四舍五入算法进行四舍五入。 否则,可能会附加零以达到精度。

      这就是为什么你会得到不必要的零和数字。

      文档建议使用 Float.toString(float)Double.toString(double) 作为值的规范表示。

      如果你想使用System.out.printf(...);,你可以用%s替换你的%f标志——在这种情况下,参数将被转换为字符串(结果是通过调用参数的toString()方法获得的,这就是你所需要的)。例如,你可以重写这一行:

      System.out.printf(x + "\t\t%f\n", population); 
      

      如下:

      System.out.printf("%d\t\t%s\n", x, population);
      

      这将打印您的population 的确切值。

      【讨论】:

      • 我会试试的。我可以通过使用println 而不是printf 来解决这个问题,但是让我看看这是否可行。谢谢!
      • @OCDkirby 我很高兴能帮上忙!此外,我还学到了一些新东西
      猜你喜欢
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      相关资源
      最近更新 更多