[英]Parse a Long number in Java
I want to parse a timestamp, a Long number like 14655**56648041**
. 我想解析一个时间戳,一个长整数,例如14655**56648041**
。 I want to create from this Long number two other Long numbers, One with the last four numbers 8041
and the second with 5664
. 我想从这个Long数字中创建另外两个Long数字,一个具有最后四个数字8041
,第二个具有5664
。 I can convert the number to String, use substring and then convert it again to Long, but I want to know if there is a cleaner way? 我可以将数字转换为String,使用子字符串,然后再次将其转换为Long,但我想知道是否有更清洁的方法? I tried using %
and /
but didn't succeed. 我尝试使用%
和/
,但未成功。
最后4位数字是num % 1E4
,前四个数字是(num % 1E8 - (num % 1E4)) / 1E4
There is. 有。 You can obtain the last four numbers by performing a modulo 10000
on the number: 您可以通过对数字执行10000
模来获得最后四个数字:
long last4 = all%10000;
You can furthermore obtain the middle four digits by first dividing by 10000
and the again perform modulo: 您还可以通过首先除以10000
并再次取模来获得中间的四位数:
long midl4 = (all/10000)%10000;
Or putting it all together: 或放在一起:
long all = 1465556648041L;
long last4 = all%10000;
long midl4 = (all/10000)%10000;
System.out.println("all="+all);
System.out.println("last4="+last4);
System.out.println("midl4="+midl4);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.