X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=994522970af119ebd8c929e318df3849c452ad67;hb=d1e63903f74f430cbd717ec23845e6ef5b3a538f;hp=ef3d84b921aea488f5ce17e5b8b52334cf24a375;hpb=a43e4f72629d04cd3122bde830b6e4925fd3aa91;p=nikiroo-utils.git
diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java
index ef3d84b..9945229 100644
--- a/src/be/nikiroo/utils/StringUtils.java
+++ b/src/be/nikiroo/utils/StringUtils.java
@@ -795,6 +795,58 @@ public class StringUtils {
return new String(unbase64(data, offset, count, zip), "UTF-8");
}
+ /**
+ * Return a display {@link String} for the given value, which can be
+ * suffixed with "k" or "M" depending upon the number, if it is big enough.
+ *
+ * Example:
+ *
+ * - 8765 becomes "8k"
+ * - 998765 becomes "998k"
+ * - 12987364 becomes "12M"
+ *
+ *
+ * @param value
+ * the value to convert
+ *
+ * @return the display value
+ */
+ public static String formatNumber(long value) {
+ return formatNumber(value, true);
+ }
+
+ /**
+ * Return a display {@link String} for the given value, which can be
+ * suffixed with "k" or "M" depending upon the number, if it is big enough.
+ *
+ * Example:
+ *
+ * - 8765 becomes "8k"
+ * - 998765 becomes "998k"
+ * - 12987364 becomes "12M"
+ *
+ *
+ * @param value
+ * the value to convert
+ * @param strict
+ * TRUE if you want any value equals or greater than 1000 to use
+ * the "k" suffix; FALSE if you want to go a bit higher and only
+ * use "k" for values equal or greater than 4000
+ *
+ * @return the display value
+ */
+ public static String formatNumber(long value, boolean strict) {
+ if (value >= 1000000l) {
+ return Long.toString(value / 1000000l) + "M";
+ }
+
+ if ((strict && value >= 1000l) || (!strict && value >= 4000l)) {
+ return Long.toString(value / 1000l) + "k";
+ }
+
+ return Long.toString(value);
+ }
+
/**
* The "remove accents" pattern.
*