简体   繁体   English

将两个 Double 相除并将 Double 商乘以 Int 时如何将 Double 转换为 Int?

[英]How to convert Double to Int when dividing two Doubles and multiplying the Double quotient by Int?

I am working on the program below.我正在研究下面的程序。 How do you calculate totalCostPerMiles = (numMiles / milesPerGallon) * price;你如何计算totalCostPerMiles = (numMiles / milesPerGallon) * price; when totalCostPerMiles is a int , numMiles and milesPerGallon are doubles , and price is a int to get the calculated value?totalCostPerMilesintnumMilesmilesPerGallondoubles ,而priceint以获得计算值时?

Suppose milesPerGallon = 30;假设milesPerGallon = 30; , numMiles = 15; , numMiles = 15; , and price = 400; , price = 400; . .

I need numMiles / milesPerGallon to output 0.5 instead of 0 and totalCostPerMiles to output 200 instead of 0.我需要numMiles / milesPerGallon输出0.5而不是 0, totalCostPerMiles输出200而不是 0。

All variables storing measured values must be doubles and all variables storing monetary values must be integers.所有存储测量值的变量必须是双精度值,所有存储货币值的变量必须是整数。

import java.util.Scanner;

public class RoadTrip {
 public static void main(String[] args) {
  
  int price, budgetPrice,
  priceInDollars, priceInCents, priceInDollarsBudget, priceInCentsBudget,
  totalCostPerMiles, dollars, cents;
  
  double milesPerGallon, numMiles, totalMilePerGall;
  
  Scanner stdin = new Scanner(System.in);
  
  milesPerGallon = stdin.nextDouble(); 
  
  numMiles = stdin.nextDouble(); 
  
  price = stdin.nextInt(); 
  
  budgetPrice = stdin.nextInt(); 
  
  
  
  
  totalCostPerMiles = (numMiles / milesPerGallon) * price; //produces error

math ops are always binary (so, 2 arguments. 'a+b+c' is not a 3-way addition; it's shorthand for (a+b)+c - 2 nested binary operations.数学运算始终是二元运算(因此,2 个参数。'a+b+c' 不是三向加法;它是(a+b)+c - 2 个嵌套二元运算的简写。

If either 'side' of the binary operation is a double, then first the other number (int, long, float, byte, char, short, doesn't matter) is converted to a double, then the operation is done, and the result is a double.如果二元运算的任一“边”是双精度数,则首先将另一个数(int、long、float、byte、char、short,无所谓)转换为双精度数,然后完成运算,然后结果是双。

So, (numMiles / milesPerGallon) is an expression of type double.因此, (numMiles / milesPerGallon)是 double 类型的表达式。

Then, you do that (so, a double), times an int: that * price .然后,你这样做(所以,一个双倍),乘以一个 int: that * price price is an int, but double*int is done by first converting that int to a double. price 是一个 int,但double*int是通过首先将该 int 转换为 double 来完成的。 Thus, the entire result is, itself, a double.因此,整个结果本身就是一个双重结果。

You then try to assign that to an int, and that's where the problem occurs: You can't do that, because java has no idea what you want.然后您尝试将它分配给一个 int,这就是问题发生的地方:您不能这样做,因为 java 不知道您想要什么。 Should 6.5 become 6, or 7? 6.5应该变成 6 还是 7? What about 1.239e123 ? 1.239e123怎么样?

You can choose what to do.你可以选择做什么。 The simplest way is the 'lop off rounding' mode, which lops off the fraction (so, round down.. unless the number is negative, in which case that'd round up):最简单的方法是“舍去舍入”模式,它舍去分数(因此,向下舍入......除非数字为负数,在这种情况下会向上舍入):

totalCostPerMilis = (int) ((numMiles / milesPerGallon) * price);

you can even use this and go with different rounding options: Add 0.5 to the mix and now it's rounding to nearest, with x.5 rounding up.你甚至可以使用它并使用不同的舍入选项:在混合中添加 0.5,现在它四舍五入到最接近的, x.5舍入。

Other stuff can be found with eg Math.round() .其他东西可以通过例如Math.round()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM