X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=36fdfbe2c967359d0677c3935bd1c56b75346b44;hb=8758aebb45238a0193b8d09b987b80ac3cdf0513;hp=994522970af119ebd8c929e318df3849c452ad67;hpb=d1e63903f74f430cbd717ec23845e6ef5b3a538f;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index 9945229..36fdfbe 100644 --- a/src/be/nikiroo/utils/StringUtils.java +++ b/src/be/nikiroo/utils/StringUtils.java @@ -812,7 +812,7 @@ public class StringUtils { * @return the display value */ public static String formatNumber(long value) { - return formatNumber(value, true); + return formatNumber(value, 0); } /** @@ -821,30 +821,115 @@ public class StringUtils { *

* Example: *

* * @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 + * @param decimalPositions + * the number of decimal positions to keep * * @return the display value */ - public static String formatNumber(long value, boolean strict) { - if (value >= 1000000l) { - return Long.toString(value / 1000000l) + "M"; + public static String formatNumber(long value, int decimalPositions) { + String suffix = ""; + String deci = ""; + + int deciDigits = 0; + if (value >= 1000000000l) { + deciDigits = (int) (value % 1000000000l); + value = value / 1000000000l; + suffix = "G"; + } else if (value >= 1000000l) { + deciDigits = (int) (value % 1000000l); + value = value / 1000000l; + suffix = "M"; + } else if (value >= 1000l) { + deciDigits = (int) (value % 1000l); + value = value / 1000l; + suffix = "k"; } - if ((strict && value >= 1000l) || (!strict && value >= 4000l)) { - return Long.toString(value / 1000l) + "k"; + if (decimalPositions > 0) { + deci = Integer.toString(deciDigits); + deci = deci.substring(0, Math.min(decimalPositions, deci.length())); + while (deci.length() < decimalPositions) { + deci += "0"; + } + deci = "." + deci; + } + + return Long.toString(value) + deci + suffix; + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value) { + return toNumber(value, 0l); + } + + /** + * The reverse operation to {@link StringUtils#formatNumber(long)}: it will + * read a "display" number that can contain a "M" or "k" suffix and return + * the full value. + *

+ * Of course, the conversion to and from display form is lossy (example: + * 6870 to "6.5k" to 6500). + * + * @param value + * the value in display form with possible "M" and "k" suffixes, + * can be NULL + * @param def + * the default value if it is not possible to convert the given + * value to a number + * + * @return the value as a number, or 0 if not possible to convert + */ + public static long toNumber(String value, long def) { + long count = def; + if (value != null) { + value = value.trim().toLowerCase(); + try { + int mult = 1; + if (value.endsWith("m")) { + value = value.substring(0, value.length() - 1).trim(); + mult = 1000000; + } else if (value.endsWith("k")) { + value = value.substring(0, value.length() - 1).trim(); + mult = 1000; + } + + long deci = 0; + if (value.contains(".")) { + String[] tab = value.split("\\."); + if (tab.length != 2) { + throw new NumberFormatException(value); + } + double decimal = Double.parseDouble("0." + + tab[tab.length - 1]); + deci = ((long) (mult * decimal)); + value = tab[0]; + } + count = mult * Long.parseLong(value) + deci; + } catch (Exception e) { + } } - return Long.toString(value); + return count; } /**