[英]How can I get the result modulo INT_MAX?
If I try to apply: Integer.valueOf(String.valueOf(Integer.MAX_VALUE) + "9");
如果我尝试应用:
Integer.valueOf(String.valueOf(Integer.MAX_VALUE) + "9");
I get a NumberFormatException
. 我得到一个
NumberFormatException
。
But I need to get the remainder modulo Integer.MAX_VALUE
from the String
representation. 但我需要从
String
表示中获取余数模数为Integer.MAX_VALUE
。
How can I achieve that? 我怎样才能做到这一点?
UPD: Integer.valueOf(String.valueOf(Integer.MAX_VALUE) + "9");
UPD:
Integer.valueOf(String.valueOf(Integer.MAX_VALUE) + "9");
is just an example. 只是一个例子。 The string can potentially have the value even more than max of Long.
该字符串的值可能超过Long的最大值。
You are looking for BigInteger
. 您正在寻找
BigInteger
。 This allows math using very large numbers. 这允许数学使用非常大的数字。
The following will allow you to do modulo with very large numbers: 以下将允许您使用非常大的数字进行模数:
BigInteger number = new BigInteger(myValueAsString);
BigInteger intMax = BigInteger.valueOf(Integer.MAX_VALUE);
BigInteger remainder = number.remainder(intMax);
To hold large number of input you can try BigInteger class. 要保存大量输入,您可以尝试BigInteger类。 create one object that will hold your String input and modulo it with second BigInteger class object that will hold MAX_VALUE of integer.
创建一个将保存String输入的对象,并使用第二个BigInteger类对象对其进行模数化,该对象将保存整数的MAX_VALUE。
You can write code as : 您可以将代码编写为:
BigInteger bigInteger = new BigInteger(str);
bigInteger = bigInteger.remainder(new BigInteger(String.valueOf(Integer.MAX_VALUE)));
System.out.println(bigInteger);
try this 试试这个
int i = (int)Long.parseLong(String.valueOf(Integer.MAX_VALUE) + "9");
note: I would use (Integer.MAX_VALUE + "9") 注意:我会使用(Integer.MAX_VALUE +“9”)
你也可以这样试试:
System.out.println(Long.valueOf(String.valueOf(Integer.MAX_VALUE + "9")) % 5);
use 使用
BigDecimal bd = new BigDecimal(str);
try the following code 尝试以下代码
int i = Integer.MAX_VALUE ;
System.out.println(i+1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.