有没有办法防止双重计算的小数输出?

Is there a way to prevent decimal output of a double calculation?

在此处获得此代码,我想知道是否可以使来自任一计算系统的输出不带 decimal/stop 小数点前的值。或者甚至将 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);
      }
    }  
}

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

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

我不建议简单地转换为 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)

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

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

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

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

您可能已经看过 'simple casting' 选项,但它看起来像这样:

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