..and the reverse operation
authorNiki Roo <niki@nikiroo.be>
Thu, 11 Apr 2019 05:05:29 +0000 (07:05 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 11 Apr 2019 05:05:29 +0000 (07:05 +0200)
src/be/nikiroo/utils/StringUtils.java

index 994522970af119ebd8c929e318df3849c452ad67..9acf3a1fd2a72d400168ba74ce515915a7e7fae7 100644 (file)
@@ -847,6 +847,63 @@ public class StringUtils {
                return Long.toString(value);
        }
 
+       /**
+        * 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.
+        * <p>
+        * Of course, the conversion to and from display form is lossy (example:
+        * <tt>6870</tt> to "6k" to <tt>6000</tt>).
+        * 
+        * @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.
+        * <p>
+        * Of course, the conversion to and from display form is lossy (example:
+        * <tt>6870</tt> to "6k" to <tt>6000</tt>).
+        * 
+        * @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) {
+                       try {
+                               if (value.toLowerCase().endsWith("m")) {
+                                       count = Long.parseLong(value.substring(0,
+                                                       value.length() - 1).trim());
+                                       count *= 1000000;
+                               } else if (value.toLowerCase().endsWith("k")) {
+                                       count = Long.parseLong(value.substring(0,
+                                                       value.length() - 1).trim());
+                                       count *= 1000;
+                               } else {
+                                       count = Long.parseLong(value);
+                               }
+                       } catch (NumberFormatException pe) {
+                       }
+               }
+
+               return count;
+       }
+
        /**
         * The "remove accents" pattern.
         *