From 8fa236df49838345d90c52850207bf2118dbbcae Mon Sep 17 00:00:00 2001 From: Laifsyn <99366187+Laifsyn@users.noreply.github.com> Date: Sat, 8 Jun 2024 21:07:36 -0500 Subject: [PATCH] Improve Beautified BigInt --- .../com/utp/clsEstructuraDiscretas/pry4/Main.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java index b8e223c..f0d3776 100644 --- a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java @@ -115,10 +115,11 @@ String pretty_big_int(BigInteger big_int) { return String.valueOf(big_int); } ArrayList triplets = new ArrayList<>(); + BigInteger acc = big_int; // Insertar Triplets en LowEndian - while (big_int.compareTo(BigInteger.ZERO) > 0) { - triplets.add(big_int.mod(BigInteger.valueOf(1000)).intValue()); - big_int = big_int.divide(BigInteger.valueOf(1000)); + while (acc.compareTo(BigInteger.ZERO) > 0) { + triplets.add(acc.mod(BigInteger.valueOf(1000)).intValue()); + acc = acc.divide(BigInteger.valueOf(1000)); } StringBuilder builder = new StringBuilder(); builder.append(triplets.remove(triplets.size() - 1)); @@ -126,6 +127,13 @@ String pretty_big_int(BigInteger big_int) { builder.append(","); builder.append(String.format("%03d", triplets.get(i))); } + String bigint_string = big_int.toString(); + final int MAX_LENGTH = 20 * 3; + if (bigint_string.length() >= MAX_LENGTH) { + String substring = builder.toString().substring(0, MAX_LENGTH); + int commas = substring.length() - substring.replace(",", "").length(); + return substring + "...+" + (commas + bigint_string.length() - MAX_LENGTH); + } return builder.toString(); }