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

huangapple go评论64阅读模式
英文:

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

问题

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

请注意,我将计算结果强制转换为整数,以满足您的要求。

英文:

I am working on the program below. How do you calculate totalCostPerMiles = (numMiles / milesPerGallon) * price; when totalCostPerMiles is a int, numMiles and milesPerGallon are doubles, and price is a int to get the calculated value?

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

I need numMiles / milesPerGallon to output 0.5 instead of 0 and totalCostPerMiles to output 200 instead of 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

答案1

得分: 1

数学运算始终是二元的(即,有两个参数)。'a+b+c' 不是三元加法;它是 (a+b)+c 的简写 - 两个嵌套的二元运算。

如果二元运算的任一“侧”是一个双精度浮点数(double),那么首先将另一个数字(int、long、float、byte、char、short,不重要)转换为双精度浮点数,然后进行运算,结果是一个double。

因此,(numMiles / milesPerGallon) 是一个 double 类型的表达式。

然后,你用这个(也就是 double),乘以一个整数:that * price。price 是一个整数,但 double*int 运算首先会将整数转换为双精度浮点数。因此,整个结果本身也是一个 double。

然后,你试图将这个赋值给一个整数,这就是问题所在:你不能这样做,因为Java 不知道你想要什么。6.5 应该变成 6 还是 7?1.239e123 呢?

你可以选择如何处理。最简单的方法是“截断舍入”模式,它会去掉小数部分(即向下取整),除非这个数是负数,在这种情况下会向上取整:

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

你甚至可以使用这个方法并选择不同的舍入选项:加上 0.5,现在就是四舍五入,对于 x.5,会向上取整。

其他的处理方式可以通过例如 Math.round() 函数找到。

英文:

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.

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.

So, (numMiles / milesPerGallon) is an expression of type double.

Then, you do that (so, a double), times an int: that * price. price is an int, but double*int is done by first converting that int to a 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. Should 6.5 become 6, or 7? What about 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.

Other stuff can be found with e.g. Math.round().

huangapple
  • 本文由 发表于 2020年9月25日 08:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定