converting string to Integer val

后端 未结 2 423
鱼传尺愫
鱼传尺愫 2021-01-25 17:07

if my String contains 100 digits or 900 digits how can i parse it to Integer value Java.

public static void main(String...args){
    long answer1=-1,answer2=-1;
         


        
2条回答
  •  太阳男子
    2021-01-25 17:34

    You cannot parse you 900 digits of number into Integer because theoretically, Integer has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (see java docs on Integer)

    Big Integer however will fulfill your use case since theoretically it has the range of -2^Integer.MAX_VALUE(2,147,483,647) to +2^Integer.MAX_VALUE(2,147,483,647).

    In your code you can just parse the string like this :

    BigInteger end = new BigInteger(endNmbr);
    

    and change all long type into BigInteger and you'll good to go.

提交回复
热议问题