[英]Type Conversion in Java Double to Int
我正在尝试将2147483648的双精度值转换为整数,进行类型转换后,我得到的输出为2147483647,该值减少1。我知道这是由于溢出而发生的,但是有一种方法可以转换它转换为int类型而不降低其精度?
As aforementioned in previous answers, you can use the long
primitive data type, or the BigInteger
reference type. 如前面的答案中所述,您可以使用long
原始数据类型或BigInteger
引用类型。 Using integral data types rather than floating points would be better for representing integers exactly. 使用整数数据类型而不是浮点数会更好地精确表示整数。
As a side note, since Java SE 8, one can use the Integer
class to use int for unsigned arithmetic. 附带说明一下,从Java SE 8开始,可以使用Integer
类将int用于无符号算术。 Per the Oracle doc - "Use the Integer class to use int data type as an unsigned integer". 根据Oracle文档-“使用Integer类将int数据类型用作无符号整数”。 Unsigned integers have a greater maximum value than int. 无符号整数的最大值比int大。 This question can be of use: Declaring an unsigned int in Java . 这个问题可能有用: 在Java中声明一个unsigned int 。
This is my first answer, hope you solved your problem! 这是我的第一个答案,希望您能解决您的问题! :) :)
I can think of 2 options. 我可以想到2个选择。
Unlike int
, which is a 32-bit signed integer data type, long
is a 64-bit signed integer data type. 与int
是32位有符号整数数据类型不同, long
是64位有符号整数数据类型。 This means that the largest value it can store is 9223372036854775807
. 这意味着它可以存储的最大值是9223372036854775807
。 The number that you are trying to store, 2147483648, is well inside that range. 您要存储的数字2147483648恰好在该范围内。
This is a reference type that represents an "immutable arbitrary-precision integer". 这是一个引用类型,表示“不可变的任意精度整数”。 You can create a BigInteger
instance that represents 2147483648 by doing 您可以通过执行以下操作创建一个代表2147483648的BigInteger
实例
new BigInteger("2147483648")
Learn more about it here: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html 在此处了解更多信息: https : //docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
The maximum integer, as indicated in the Integer class by the static final int MAX_VALUE
, is 2^31-1 (2,147,483,647). 如Integer类中的static final int MAX_VALUE
所示,最大整数为2 ^ 31-1(2,147,483,647)。 This value is the maximum integer as it is the largest 32-bit signed integer. 该值是最大整数,因为它是最大的32位有符号整数。
The maximum double, as indicated in the Double class by the static final int MAX_VALUE
, is (2-2^(-52))(2^1023). 如Double类中由static final int MAX_VALUE
指示的最大double为(2-2 ^(-52))(2 ^ 1023)。 The double data type follows the double-precision 64-bit IEEE 754 floating point format to express a wide range of dynamic numerical values. double数据类型遵循双精度64位IEEE 754浮点格式,以表示各种动态数值。
In you're observation, you have a double with a value 2,147,483,648 which you attempt to convert to an integer by type casting. 在观察中,您有一个值为2,147,483,648的双精度型,试图通过类型转换将其转换为整数。
double d = 2147483648;
int i = (int) d;
Casting between primitive types allows your to convert the value of one primitive type to another. 在原始类型之间进行转换可以使您将一种原始类型的值转换为另一种原始类型。 Converting from a double to an integer is known as a Narrowing Primitive Conversion , wherein you: 从双精度整数转换为整数称为Narrowing Primitive Conversion ,其中:
"may lose information about the overall magnitude of a numeric value and may also lose precision and range." “可能会丢失有关数值总体大小的信息,也可能会丢失精度和范围。”
The narrowing conversion of a floating-point number to an int is as follows: 浮点数到int的缩小转换如下:
With the integer value of the double value larger than the Integer.MAX_VALUE, in order to allow for representation as an integer the value of Integer.MAX_VALUE is used. 如果double值的整数值大于Integer.MAX_VALUE,则为了允许表示为整数,使用Integer.MAX_VALUE的值。
To avoid the loss of precision you will need to either cast to a primitive type or a numeric Object wherein the maximum value is greater than 2147483648 (and resolution allows for accuracy to be maintained). 为避免精度损失,您将需要转换为原始类型或数字对象,其中最大值大于2147483648(并且分辨率可保持准确性)。
The long primitive type has a maximum value of 2^63-1 (9.223372e+18), which would be a good choice if you want to use numbers within the numeric integer space. long基本类型的最大值为2 ^ 63-1(9.223372e + 18),如果要在数字整数空间内使用数字,则是个不错的选择。 Note that while the Long.MAX_VALUE is very large, Double.MAX_VALUE is MUCH larger due to the floating-point format. 请注意,虽然Long.MAX_VALUE非常大,但由于浮点格式,Double.MAX_VALUE会大很多。
double d = 2147483648;
long i = (long) d;
Converting from a double
to an int
will lose precision for numbers greater than Integer.MAX_VALUE
or less that Integer.MIN_VALUE
. 从转换double
到int
将失去精度数大于Integer.MAX_VALUE
或小于Integer.MIN_VALUE
。 There is no way to represent numbers outside that range as an int
. 无法将超出该范围的数字表示为int
。 It is mathematically impossible. 从数学上讲这是不可能的。
Converting from a double
to an long
will also lose precision. 从double
精度型转换为long
精度也会降低精度。 This will occur for all integers outside of Long.MIN_VALUE
through Long.MAX_VALUE
. 对于Long.MIN_VALUE
到Long.MAX_VALUE
之外的所有整数,都会发生这种情况。 But a second problem is that double
itself is not able to represent all integers in that range ... so there will be loss of precision before the conversion 1 . 但是第二个问题是double
本身不能表示该范围内的所有整数...因此在转换1 之前会失去精度。
Don't use floating point numbers if you need to represent integers precisely. 如果需要精确表示整数,请不要使用浮点数。 Use an integral type ( byte
, short
, char
, int
or long
) ... or BigInteger
. 使用整数类型( byte
, short
, char
, int
或long
)...或BigInteger
。
1 - A double
is a 64 bit IEE floating point number, which has 52 bits of precision and a sign bit. 1- double
是64位IEE浮点数,具有52位精度和一个符号位。 By contrast, a long
has 64 bits of precision (including sign). 相比之下, long
的精度为64位(包括符号)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.