将 int 类型转换为双 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39709217/
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
type casting int to double java?
提问by Nocturnel
I'm practicing debugging and there is a question that i'm unsure on, it's asking what is wrong and what can I do to fix it I read the book chapter on it but not specific enough. pasted from the book:
我正在练习调试,但有一个我不确定的问题,它询问出了什么问题,我可以做些什么来解决它,我阅读了有关它的书籍章节,但不够具体。从书中粘贴:
int a = 26.4 ^ When you compile, this is the message:
int a = 26.4 ^ 编译时,这是消息:
Test.Java:8: possible loss of precision int a = 26.4;
Test.Java:8: 可能损失精度 int a = 26.4;
required: int found : double 1 error
要求:int found:double 1 错误
I have a decent understanding on why there is an error, because of how double has a higher precedence than int and how an int cant necessarily store a double value.
我对为什么会出现错误有很好的理解,因为 double 的优先级高于 int 以及 int 不一定存储 double 值。
My question is, is there a way to type cast variable a into a double type? Or is the only way to fix this by changing a from int to double?
我的问题是,有没有办法将强制转换变量 a 键入双精度类型?或者是通过将 a 从 int 更改为 double 来解决此问题的唯一方法?
thanks
谢谢
采纳答案by Keiwan
The only possibilities you have are:
你唯一的可能性是:
Type cast the
double
into anint
if you want to store your number as an integer:int a = (int)26.4 // so a will be 26
(you will obviously lose precision this way)
Store the number as a
double
to keep the precision:double a = 26.4
类型强制转换
double
成一个int
,如果你想你的电话号码存储为一个整数:int a = (int)26.4 // so a will be 26
(你显然会以这种方式失去精度)
将数字存储为 a
double
以保持精度:double a = 26.4
回答by Wojciech Kazior
Casting will not help at anything, look at the code below:
铸造无济于事,请看下面的代码:
//int a = 26.4; // gives compile error
int a = (int) 26.4; // gives 26
double b = a; // gives 26.0
double c = (double) a; // also gives 26.0
回答by Code-Apprentice
I have a decent understanding on why there is an error, because of how double has a higher precedence than int and how an int cant necessarily store a double value.
我对为什么会出现错误有很好的理解,因为 double 的优先级高于 int 以及 int 不一定存储 double 值。
Although "precedence" isn't the correct terminology, you seem to understand at the problem at some basic level. When you cast a double
to an int
, you can lose information. In this case, 26.4
will be truncated to 26
.
尽管“优先级”不是正确的术语,但您似乎在某些基本层面上理解了这个问题。当您将 a 转换double
为 an 时int
,您可能会丢失信息。在这种情况下,26.4
将被截断为26
。
My question is, is there a way to type cast variable a into a double type?
我的问题是,有没有办法将强制转换变量 a 键入双精度类型?
A typecast changes the type of a valuefrom one type to another. You cannot use typecasting to change the type of a variable. Once a
is declared as an int
, it is always an int
. You cannot change that. However, the value stored in a
can be cast to other types when it is used in expressions.
类型转换将值的类型从一种类型更改为另一种类型。您不能使用类型转换来更改变量的类型。一旦a
被声明为 an int
,它始终是 an int
。你不能改变这一点。但是,存储在的值a
在表达式中使用时可以转换为其他类型。
回答by erickson
There are a few ways to fix this, and which is right depends on the application.
有几种方法可以解决这个问题,哪种方法正确取决于应用程序。
The first to to keep the left-hand type as int
, and cast the right-hand side to int
:
第一个 to 将左侧类型保持为int
,并将右侧类型转换为int
:
int a = (int) 26.4;
This is right if rounding toward zero, and limiting values to the range Integer.MIN_VALUE
and Integer.MAX_VALUE
is okay for your application.
如果向零四舍五入并将值限制在范围内Integer.MIN_VALUE
,这是正确的,并且Integer.MAX_VALUE
适用于您的应用程序。
Next, you might change the type of the left-hand side to double
.
接下来,您可以将左侧的类型更改为double
。
double a = 26.4;
This would be right if further operations using a
can be performed with double
. This won't work if you have to have an int
value, for example, in order to pass to some API that requires int
, or to specify the length or index of an array.
如果a
可以使用double
. 如果您必须有一个int
值,例如,为了传递给某些需要 的 API int
,或者指定数组的长度或索引,这将不起作用。
Or, you might need to explicitly control the rounding and range checking using the Math
API. For example, to round to the nearestinteger, and throw an exception if the value is too large for an int
:
或者,您可能需要使用Math
API显式控制舍入和范围检查。例如,四舍五入到最接近的整数,如果值对于 来说太大,则抛出异常int
:
int a = Math.toIntExact(Math.round(26.4));
回答by krishankantray
I think the follow code may help you. In the below code, it is converting a double into an integer variable, although it is a little tedious process for such a simple thing.
我认为以下代码可能对您有所帮助。在下面的代码中,它正在将一个 double 转换为一个整数变量,尽管对于这样一个简单的事情来说这是一个有点乏味的过程。
double a=26.4;
int b =(int)Math.round(a);
double a=26.4;
int b =(int)Math.round(a);