Java JDK - 从 double 到 int 的可能有损转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34396577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java JDK - possible lossy conversion from double to int
提问by
So I have recently written the following code:
所以我最近写了以下代码:
import java.util.Scanner;
public class TrainTicket
{
public static void main (String args[])
{
Scanner money = new Scanner(System.in);
System.out.print("Please type in the type of ticket you would like to buy.\nA. Child B. Adult C. Elder.");
String type = money.next();
System.out.print("Now please type in the amount of tickets you would like to buy.");
int much = money.nextInt();
int price = 0;
switch (type)
{
case "A":
price = 10;
break;
case "B":
price = 60;
break;
case "C":
price = 35;
break;
default:
price = 0;
System.out.print("Not a option ;-;");
}
if (price!=0)
{
int total2 = price* much* 0.7;
System.out.print("Do you have a coupon code? Enter Y or N");
String YN = money.next();
if (YN.equals("Y"))
{
System.out.print("Please enter your coupon code.");
int coupon = money.nextInt();
if(coupon==21)
{
System.out.println("Your total price is " + "$" + total2 + ".");
}
else
{
System.out.println("Invalid coupon code, your total price is " + "$" + price* much + ".");
}
}
else
{
System.out.println("Your total price is " + "$" + price* much + "." );
}
}
money.close();
}
}
However, it keeps displaying this:
但是,它一直显示:
TrainTicket.java:31: error: incompatible types: possible lossy conversion from double to int
int total2 = price* much* 0.7;
When I try and run it with cmd.
当我尝试使用 cmd 运行它时。
Can someone help and explain the error that I have made? Any help is appreciated :). Thanks!
有人可以帮忙解释一下我犯的错误吗?任何帮助表示赞赏:)。谢谢!
采纳答案by Mathews Mathai
When you convert double
to int
,the precision of the value is lost.
For example,
When you convert 4.8657 (double) to int.The int value will be 4.Primitive int
does not store decimal numbers.So you will lose 0.8657.
当您转换double
为 时int
,该值的精度将丢失。例如,当您将 4.8657 (double) 转换为 int 时,int 值将为 4。Primitiveint
不存储十进制数。因此您将损失 0.8657。
In your case,0.7 is a double value(floating point treated as double by default unless mentioned as float-0.7f).
When you calculate price*much*0.7
,the answer is a double value and so the compiler wouldn't allow you to store it in a type integer since there could be a loss of precision.So that's what is possible lossy conversion
,you may lose precision.
在您的情况下,0.7 是一个双精度值(浮点数默认被视为双精度值,除非提到 float-0.7f)。当你计算时price*much*0.7
,答案是一个双精度值,所以编译器不允许你将它存储在一个整数类型中,因为可能possible lossy conversion
会丢失精度。这就是你可能会丢失精度。
So what could you do about it? You need to tell the compiler that you really want to do it.You need to tell it that you know what you are doing. So explicitly convert double to int using the following code:
那么你能做些什么呢?你需要告诉编译器你真的想这样做。你需要告诉它你知道你在做什么。因此,使用以下代码将 double 显式转换为 int:
int total2= (int) price*much*0.7;
/*(int) tells compiler that you are aware of what you are doing.*/
//also called as type casting
In your case,since you are calculating the cost,I'll suggest you to declare variable total2
as the type double or float.
在您的情况下,由于您正在计算成本,我建议您将变量声明total2
为 double 或 float 类型。
double total2=price*much*0.7;
float total2=price*much*0.7;
//will work
回答by khelwood
You are trying to assign price* much* 0.7
, which is a floating point value (a double
), to an integer variable. A double
is not an exact integer, so in general an int
variable cannot hold a double
value.
您正在尝试将price* much* 0.7
浮点值 (a double
)分配给整数变量。Adouble
不是一个精确的整数,所以通常一个int
变量不能保存一个double
值。
For instance, suppose the result of your calculation is 12.6
. You can't hold 12.6
in an integer variable, but you could cast away the fraction and just store 12
.
例如,假设您的计算结果是12.6
。您不能保存12.6
整数变量,但您可以丢弃分数并只存储12
.
If you are not worried about the fraction you will lose, cast your number to an int
like this:
如果你不担心你会失去的分数,把你的数字变成int
这样:
int total2 = (int) (price* much* 0.7);
Or you could round it to the nearest integer.
或者您可以将其四舍五入到最接近的整数。
int total2 = (int) Math.round(price*much*0.7);