KillerCode
KillerCode

Reputation: 41

Split BigInteger to specific number of digits

I want to split BigInteger number (1612513671536531537631747547475745713467754864474894674969787486558856856587856595095785859828347864647647545474665743827865765785635689266855775685657587586565858655985865858568585856858)

into 20 digit numbers.How can I do this?Thank you very much.

Upvotes: 0

Views: 2062

Answers (3)

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Cast it to a String and then use

String strBigInt = <yourBigInt>.toString();
List<String> parts = new ArrayList<String>();
while(strBigInt.length > 20){
    parts.add(strBigInt.substring(0,20));
    strBigInt = strBigInt.substring(20);
}
parts.add(strBigInt);

Upvotes: 0

someone
someone

Reputation: 6572

You could try to split it as bellow after taking it as a String

String s = "1612513671536531537631747547475745713467754864474894674969787486558856856587856595095785859828347864647647545474665743827865765785635689266855775685657587586565858655985865858568585856858";
    String [] numbers =s.split("(?<=\\G.{20})");


for(String num:numbers){
        System.out.println(num);
    }

and out put

16125136715365315376
31747547475745713467
75486447489467496978
74865588568565878565
95095785859828347864
64764754547466574382
78657657856356892668
55775685657587586565
85865598586585856858
5856858

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

12345678901234567891 should become 1234567890123456789 and 1

String s = bi.toString();
List<String> parts = new ArrayList<String>();
for (int i = 0; i < s.length(); i += 20)
    parts.add(s.substring(i, Math.min(i + 20, s.length())));
System.out.println(parts);

prints

[16125136715365315376, 31747547475745713467, 75486447489467496978, 74865588568565878565, 95095785859828347864, 64764754547466574382, 78657657856356892668, 55775685657587586565, 85865598586585856858, 5856858]

Upvotes: 0

Related Questions