X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=1ee9ac4b569001fda8036815fe8a43277e8e18ef;hb=ce3972ea1a7083d1f8ef17463fb48c5bfa7ebf73;hp=36fdfbe2c967359d0677c3935bd1c56b75346b44;hpb=8758aebb45238a0193b8d09b987b80ac3cdf0513;p=fanfix.git
diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java
index 36fdfbe..1ee9ac4 100644
--- a/src/be/nikiroo/utils/StringUtils.java
+++ b/src/be/nikiroo/utils/StringUtils.java
@@ -799,11 +799,13 @@ public class StringUtils {
* 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:
+ *
+ * Examples:
*
- * - 8765 becomes "8k"
- * - 998765 becomes "998k"
- * - 12987364 becomes "12M"
+ * - 8 765 becomes "8k"
+ * - 998 765 becomes "998k"
+ * - 12 987 364 becomes "12M"
+ * - 5 534 333 221 becomes "5G"
*
*
* @param value
@@ -819,11 +821,12 @@ public class StringUtils {
* 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:
+ * Examples (assuming decimalPositions = 1):
*
- * - 8765 becomes "8.7k"
- * - 998765 becomes "998.7k"
- * - 12987364 becomes "12.9M"
+ * - 8 765 becomes "8.7k"
+ * - 998 765 becomes "998.7k"
+ * - 12 987 364 becomes "12.9M"
+ * - 5 534 333 221 becomes "5.5G"
*
*
* @param value
@@ -834,34 +837,41 @@ public class StringUtils {
* @return the display value
*/
public static String formatNumber(long value, int decimalPositions) {
+ long userValue = value;
String suffix = "";
- String deci = "";
+ long mult = 1;
- int deciDigits = 0;
if (value >= 1000000000l) {
- deciDigits = (int) (value % 1000000000l);
- value = value / 1000000000l;
+ mult = 1000000000l;
+ userValue = value / 1000000000l;
suffix = "G";
} else if (value >= 1000000l) {
- deciDigits = (int) (value % 1000000l);
- value = value / 1000000l;
+ mult = 1000000l;
+ userValue = value / 1000000l;
suffix = "M";
} else if (value >= 1000l) {
- deciDigits = (int) (value % 1000l);
- value = value / 1000l;
+ mult = 1000l;
+ userValue = value / 1000l;
suffix = "k";
}
+ String deci = "";
if (decimalPositions > 0) {
- deci = Integer.toString(deciDigits);
+ deci = Long.toString(value % mult);
+ int size = Long.toString(mult).length() - 1;
+ while (deci.length() < size) {
+ deci = "0" + deci;
+ }
+
deci = deci.substring(0, Math.min(decimalPositions, deci.length()));
while (deci.length() < decimalPositions) {
deci += "0";
}
+
deci = "." + deci;
}
- return Long.toString(value) + deci + suffix;
+ return Long.toString(userValue) + deci + suffix;
}
/**
@@ -904,8 +914,11 @@ public class StringUtils {
if (value != null) {
value = value.trim().toLowerCase();
try {
- int mult = 1;
- if (value.endsWith("m")) {
+ long mult = 1;
+ if (value.endsWith("g")) {
+ value = value.substring(0, value.length() - 1).trim();
+ mult = 1000000000;
+ } else if (value.endsWith("m")) {
value = value.substring(0, value.length() - 1).trim();
mult = 1000000;
} else if (value.endsWith("k")) {