【问题标题】:Is there a way to prevent decimal output of a double calculation?有没有办法防止双重计算的十进制输出?
【发布时间】:2021-05-13 13:32:51
【问题描述】:

在此处获得此代码,我想知道是否可以使来自任一计算系统的输出不带小数/将值停止在小数点之前的位置。甚至将 double 转换为 int 而不会出现任何错误。

我知道,请忽略开始时毫无意义的 do while 循环。

感谢您的帮助。

class Main{
  public static void main(String[] args)
  {
    calculation(getSystemChoice());
  }
    public static int getSystemChoice()
    {
      Scanner input = new Scanner(System.in);  //create scanner
      int systemChoice;

      do{
    System.out.println("If you are using the Metric system, please enter a 1.");
    System.out.println("If you are using the Imperial system, please enter a 2.");
    System.out.println("To quit the program, please enter a 3.");
    systemChoice = input.nextInt();
    //Switch start
    switch(systemChoice){
      case 1:
        systemChoice=1;
        return systemChoice;
      case 2:
        systemChoice=2;
        return systemChoice;
      default:  //Currently no working input correction system, likely due to no case for 3. !!!! 
        System.exit(0);
    }
      //Switch End
      }
      while(systemChoice != 1 || systemChoice != 2 || systemChoice != 3);
      return systemChoice;
    }

    //This method takes an int as a parameter(1 or 2) and runs if statements based on the metric or imperial systems.
    public static void calculation(int systemChoice)
    {
      double inches, centimeters, meters, feet;
      Scanner input = new Scanner(System.in);  //create scanner
      //if the user entered one, the result will be in meters and centimeters
      if(systemChoice == 1){
      System.out.print("Enter amount of meters: ");
     meters = input.nextDouble();
      System.out.print("Enter amount of centimeters: ");
      centimeters = input.nextDouble();
      feet = meters * 3.28084;
     inches = centimeters / 2.54;
     System.out.printf("Feet: %.2f\t " , feet);  
     System.out.printf("Inches: %.2f\t " , inches);
    rerun(systemChoice);
    }
      // if the user entered 2 then the result will be in feet and inches
    else if(systemChoice == 2){
      System.out.print("Enter amount of feet: ");
      feet = input.nextDouble();
      System.out.print("Enter amount of inches: ");
      inches = input.nextDouble();
      meters = feet / 3.28084;
      centimeters = inches * 2.54;
      System.out.printf("Meters: %.2f\t " , meters);  
      System.out.printf("Centimeters: %.2f\t\n " , centimeters);
      rerun(systemChoice);
    }
    }
    public static void rerun(int systemChoice)
    {
      Scanner in = new Scanner(System.in);
      System.out.println("\nIf you would like to make another measurement, enter 4.");
      System.out.println("Otherwise, you may quit by entering any other number.");
          systemChoice = in.nextInt();
      if(systemChoice == 4)
      {
        getSystemChoice();
        calculation(systemChoice);
      }
      else
      {
        System.exit(0);
      }
    }  
}

【问题讨论】:

    标签: java math type-conversion calculation


    【解决方案1】:

    您可以在打印之前使用强制转换并将其打印为整数。

    System.out.printf("Inches: %d " , (int)inches)
    

    【讨论】:

      【解决方案2】:

      我不建议简单地转换为 int。这是一个例子:

      double myValue = 8.65;
      System.out.println((int) myValue); // will output 8 as java will always round to the next lower integer
      System.out.println(Math.round(myValue)); // will output 9 which obviously is correct (mathematically speaking)
      

      【讨论】:

      • 注:不过,有很多可能的舍入模式。什么是“正确”取决于您的要求是什么......
      【解决方案3】:

      根据您的具体要求,有许多潜在的解决方案。其他海报已经提到了一对。值得牢记每种方法的优缺点:

      • 简单地转换为 int 或 long 是最简单的方法,但始终会向下舍入。这对于您的训练示例可能没问题。但在现实世界的应用程序中,这可能会导致一些细微的错误,其值可以用 double 表示,但 int 或 long 不能(例如,double 可以将 1.0/0 的结果表示为“无穷大”,但转换为 int 或long 会将其转换为一个大的正整数值——这可能会导致实际应用程序中出现细微的错误);
      • 您可以使用 Math.round() 来使用向上或向下舍入到“最近”整数的约定;但这并不能解决无法表示的值的问题;
      • 对于其他舍入模式,您可以使用 BigDecimal 类:请参阅 BigDecimal.round() 方法 - 许多应用程序不需要此方法,但某些特殊情况可能需要;
      • 要在处理“特殊”值的同时将输出截断为零小数位,您可以使用 String.format() 并指定零小数位。

      后一个选项的代码如下所示:

      double val = ...
      System.out.printf("Truncated value is: %.0f", val);
      

      您可能已经看过“简单转换”选项,但它看起来像这样:

      double val = ...
      long truncatedVal = (long) val;
      System.out.println("Truncated value = " + truncatedVal);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多