From 9d593231fc55d29a2b1022cced2f6b46eb1ca274 Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Sat, 31 Aug 2024 12:09:14 -0400 Subject: [PATCH] Add `StringUtility#formatNumber(Number)` --- .../srnyx/javautilities/StringUtility.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/xyz/srnyx/javautilities/StringUtility.java b/src/main/java/xyz/srnyx/javautilities/StringUtility.java index bca806e..cd5d012 100644 --- a/src/main/java/xyz/srnyx/javautilities/StringUtility.java +++ b/src/main/java/xyz/srnyx/javautilities/StringUtility.java @@ -25,10 +25,10 @@ public static String repeat(@NotNull CharSequence charSequence, int amount) { } /** - * Formats a {@link Double} value using the given pattern + * Formats a {@link Number} value using the given pattern * * @param value the {@link Number} to format - * @param pattern the pattern to use + * @param pattern the pattern to use (if null: {@code #,###.##}) * * @return the formatted value */ @@ -39,15 +39,28 @@ public static String formatNumber(@NotNull Number value, @Nullable String patter } /** - * Shortens a string to a given length, adding "..." at the end ("..." is included in the length) + * Formats a {@link Number} value using {@code #,###.##} + * + * @param value the {@link Number} to format + * + * @return the formatted value + */ + @NotNull + public static String formatNumber(@NotNull Number value) { + return formatNumber(value, null); + } + + /** + * Shortens a string to a given length, adding {@code ...} at the end (included in the length) * * @param string the string to shorten * @param length the length to shorten to * - * @return the shortened string + * @return the shortened string with {@code ...} at the end */ @NotNull public static String shorten(@NotNull String string, int length) { + if (length < 3) throw new IllegalArgumentException("Length must be at least 3"); return string.length() + 3 > length ? string.substring(0, length - 3) + "..." : string; }