如何在Java中将long转换为int?
答案 0 :(得分:277)
在Java 8中更新:
Math.toIntExact(value);
原始答案:
简单类型转换应该这样做:
long l = 100000;
int i = (int) l;
但请注意,大数字(通常大于2147483647
且小于-2147483648
)会丢失部分内容,并且会被错误地表示。
例如,2147483648
将表示为-2147483648
。
答案 1 :(得分:164)
Long x = 100L;
int y = x.intValue();
的
答案 2 :(得分:56)
对于较小的值,施法就足够了:
long l = 42;
int i = (int) l;
但是,long
可以包含的信息多于int
,因此在一般情况下,无法完全从long
转换为int
。如果long
包含小于或等于Integer.MAX_VALUE
的数字,您可以通过投射转换它而不会丢失任何信息。
例如,以下示例代码:
System.out.println( "largest long is " + Long.MAX_VALUE );
System.out.println( "largest int is " + Integer.MAX_VALUE );
long x = (long)Integer.MAX_VALUE;
x++;
System.out.println("long x=" + x);
int y = (int) x;
System.out.println("int y=" + y);
在我的机器上产生以下输出:
largest long is 9223372036854775807
largest int is 2147483647
long x=2147483648
int y=-2147483648
注意y
上的负号。由于x
的值大于Integer.MAX_VALUE,因此int y
无法保留它。在这种情况下,它缠绕在负数上。
如果您想自己处理此案例,可以执行以下操作:
if ( x > (long)Integer.MAX_VALUE ) {
// x is too big to convert, throw an exception or something useful
}
else {
y = (int)x;
}
所有这些都假定为正数。对于负数,请使用MIN_VALUE
而不是“MAX_VALUE”
答案 3 :(得分:49)
自Java 8以来 你可以使用:Math.toIntExact(long value)
返回long参数的值;如果值溢出int,则抛出异常。
JDK 8中Math.toIntExact
的源代码:
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}
答案 4 :(得分:34)
如果使用Guava library,则有Ints.checkedCast(long)
和Ints.saturatedCast(long)
方法可将long
转换为int
。
答案 5 :(得分:21)
long x = 3;
int y = (int) x;
但是假设long
可以表示为int
,你知道两者之间的区别吗?
答案 6 :(得分:15)
您可以使用Long包装器而不是long原语并调用Long.intValue() http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#intValue()
它相应地舍入/截断long值以适合int。
答案 7 :(得分:3)
如果直接投射显示错误,您可以这样做:
>>> a = [ 'i am going to America', 'i will come by tomorrow evening' ]
>>> [[x] for x in a]
[['i am going to America'], ['i will come by tomorrow evening']]
答案 8 :(得分:2)
在Java中,long是带符号的64位数字,这意味着您可以存储-9,223,372,036,854,775,808和9,223,372,036,854,775,807(含)之间的数字。
另一方面,int是32位数字的签名,这意味着你可以存储-2,147,483,648和2,147,483,647(含)之间的数字。
因此,如果您的long超出了int允许的值,那么您将无法获得有价值的转换。
此处有关原始Java类型大小的详细信息:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
答案 9 :(得分:1)
在 Java 8 中,我按照以下方式进行操作
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<footer class="form__footer">
<div class="form__actions">
<div class="form__action form__action--primary">
<button class="button button--chevron-right button--primary" name="next" type="submit" value="Next">Next
</button>
</div>
<div class="form__action form__action--secondary">
<button class="button button--chevron-left button--standout" name="back" type="submit" value="Previous">Previous
</button>
</div>
</div>
</footer>
如果long l = 100L;
int i = Math.toIntExact(l);
的值超出ArithmaticException
的范围,则此方法将引发long
。因此,我可以避免数据丢失。
答案 10 :(得分:1)
可以在此处进行手动类型转换:
long x1 = 1234567891;
int y1 = (int) x1;
System.out.println("in value is " + y1);
答案 11 :(得分:0)
long x = 3.120;
int z = x.intValue();
如果您需要直接进行转换
longvalue.intvalue();
答案 12 :(得分:0)
如果您想进行安全的转换并喜欢将Java8与Lambda表达式一起使用 您可以像这样使用它:
val -> Optional.ofNullable(val).map(Long::intValue).orElse(null)
答案 13 :(得分:0)
最短,最安全,最简单的解决方案是:
long myValue=...;
int asInt = Long.valueOf(myValue).intValue();
请注意,Long.valueOf
的行为如下:
使用此代码:
System.out.println("Long max: " + Long.MAX_VALUE);
System.out.println("Int max: " + Integer.MAX_VALUE);
long maxIntValue = Integer.MAX_VALUE;
System.out.println("Long maxIntValue to int: " + Long.valueOf(maxIntValue).intValue());
long maxIntValuePlusOne = Integer.MAX_VALUE + 1;
System.out.println("Long maxIntValuePlusOne to int: " + Long.valueOf(maxIntValuePlusOne).intValue());
System.out.println("Long max to int: " + Long.valueOf(Long.MAX_VALUE).intValue());
结果为:
Long max: 9223372036854775807
Int max: 2147483647
Long max to int: -1
Long maxIntValue to int: 2147483647
Long maxIntValuePlusOne to int: -2147483648
答案 14 :(得分:0)
我也遇到过这个问题。 为了解决这个问题,我首先将 long 转换为 String,然后转换为 int。
int i = Integer.parseInt(String.valueOf(long));
答案 15 :(得分:0)
long x;
int y;
y = (int) x
只要数字小于 2147483647 且没有错误,您就可以将 long 转换为 int。
答案 16 :(得分:-4)
Long l = 100;
int i = Math.round(l);
答案 17 :(得分:-7)
在Spring中,有一种严格的方法可以将long转换为int
不仅lnog可以转换为int,任何类型的扩展Number一般可以转换为其他Number类型,这里我将向您展示如何将long转换为int,反之亦然。
Long l = 1234567L;
int i = org.springframework.util.NumberUtils.convertNumberToTargetClass(l, Integer.class);