Skip to content

Commit

Permalink
Add StringUtility#formatNumber(Number)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Aug 31, 2024
1 parent 4c06546 commit 9d59323
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/xyz/srnyx/javautilities/StringUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

Expand Down

0 comments on commit 9d59323

Please sign in to comment.