Skip to content

Commit 9d59323

Browse files
committed
Add StringUtility#formatNumber(Number)
1 parent 4c06546 commit 9d59323

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/main/java/xyz/srnyx/javautilities/StringUtility.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public static String repeat(@NotNull CharSequence charSequence, int amount) {
2525
}
2626

2727
/**
28-
* Formats a {@link Double} value using the given pattern
28+
* Formats a {@link Number} value using the given pattern
2929
*
3030
* @param value the {@link Number} to format
31-
* @param pattern the pattern to use
31+
* @param pattern the pattern to use (if null: {@code #,###.##})
3232
*
3333
* @return the formatted value
3434
*/
@@ -39,15 +39,28 @@ public static String formatNumber(@NotNull Number value, @Nullable String patter
3939
}
4040

4141
/**
42-
* Shortens a string to a given length, adding "..." at the end ("..." is included in the length)
42+
* Formats a {@link Number} value using {@code #,###.##}
43+
*
44+
* @param value the {@link Number} to format
45+
*
46+
* @return the formatted value
47+
*/
48+
@NotNull
49+
public static String formatNumber(@NotNull Number value) {
50+
return formatNumber(value, null);
51+
}
52+
53+
/**
54+
* Shortens a string to a given length, adding {@code ...} at the end (included in the length)
4355
*
4456
* @param string the string to shorten
4557
* @param length the length to shorten to
4658
*
47-
* @return the shortened string
59+
* @return the shortened string with {@code ...} at the end
4860
*/
4961
@NotNull
5062
public static String shorten(@NotNull String string, int length) {
63+
if (length < 3) throw new IllegalArgumentException("Length must be at least 3");
5164
return string.length() + 3 > length ? string.substring(0, length - 3) + "..." : string;
5265
}
5366

0 commit comments

Comments
 (0)