I have a method (in 3rd-party library) with BigInteger parameter:
public void setValue (BigInteger value) { ... }
I don't need 'all its power', I only need to work with integers. So, how can I pass integers to this method? My solution is to get string value from int value and then create BigInteger from string:
int i = 123;
setValue (new BigInteger ("" + i));
Are there any other (recommended) ways to do that?
Use different radix to create BigInteger from String BigInteger(String val, int radix) translates the String representation of a BigInteger in the specified radix into a BigInteger. You can set the radix for the string value passed in with new BigInteger(String val, int radix) .
Initializing a BigInteger from a String Now that we are able to represent numerical numbers using Strings, we have raised the maximum number we can initialize a big integer to a number with 2147483647 digits. This is because the maximum length of a String is Integer. MAX_VALUE.
The simpler "proper" way would be String.
BigInteger.valueOf(i);
Use the static method BigInteger.valueOf(long number)
. int
values will be promoted to long
automatically.
You can use this static method: BigInteger.valueOf(long val)
setValue(BigInteger.valueOf(123L));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With