Version 4.6.1
authorNiki Roo <niki@nikiroo.be>
Fri, 12 Apr 2019 15:41:08 +0000 (17:41 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 12 Apr 2019 15:41:08 +0000 (17:41 +0200)
VERSION
changelog.md
src/be/nikiroo/utils/StringUtils.java
src/be/nikiroo/utils/test/StringUtilsTest.java
src/be/nikiroo/utils/test/Test.java

diff --git a/VERSION b/VERSION
index 6016e8addc4d0ea9fab0f803ecb49f93541fd44f..8ac28bf9f0f0ea8466032de41a817507691422db 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-4.6.0
+4.6.1
index 62e0e65ea14f56c48fd8582241a1ce52abb0cd01..4c30d16b6a366cd4a0d6b84a4cdded31e73068b4 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
+## Version 4.6.1
+
+- changed: formatNumber/toNumber and decimals
+
 ## Version 4.6.0
 
 - new: proxy
index 9acf3a1fd2a72d400168ba74ce515915a7e7fae7..21261117f0205010c30016af01d4c14eface39d6 100644 (file)
@@ -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,44 @@ public class StringUtils {
         * <p>
         * Example:
         * <ul>
-        * <li><tt>8765</tt> becomes "8k"</li>
-        * <li><tt>998765</tt> becomes "998k"</li>
-        * <li><tt>12987364</tt> becomes "12M"</li>
+        * <li><tt>8765</tt> becomes "8.7k"</li>
+        * <li><tt>998765</tt> becomes "998.7k"</li>
+        * <li><tt>12987364</tt> becomes "12.9M"</li>
         * </ul>
         * 
         * @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) {
+       public static String formatNumber(long value, int decimalPositions) {
+               String suffix = "";
+               String deci = "";
+
+               int deciDigits = 0;
                if (value >= 1000000l) {
-                       return Long.toString(value / 1000000l) + "M";
+                       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);
+               return Long.toString(value) + deci + suffix;
        }
 
        /**
@@ -853,7 +867,7 @@ public class StringUtils {
         * 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>).
+        * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
         * 
         * @param value
         *            the value in display form with possible "M" and "k" suffixes,
@@ -871,7 +885,7 @@ public class StringUtils {
         * 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>).
+        * <tt>6870</tt> to "6.5k" to <tt>6500</tt>).
         * 
         * @param value
         *            the value in display form with possible "M" and "k" suffixes,
@@ -885,19 +899,30 @@ public class StringUtils {
        public static long toNumber(String value, long def) {
                long count = def;
                if (value != null) {
+                       value = value.trim().toLowerCase();
                        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);
+                               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];
                                }
-                       } catch (NumberFormatException pe) {
+                               count = mult * Long.parseLong(value) + deci;
+                       } catch (Exception e) {
                        }
                }
 
index 5280181b9dbc94db92b76d6f62461d55a60f456c..df2ed5fb9ddb49b2afd7dfa6133424f26fc9648c 100644 (file)
@@ -228,33 +228,43 @@ class StringUtilsTest extends TestLauncher {
                                assertEquals(orig, unzipped);
                        }
                });
-               
-               addTest(new TestCase("from/toNumber") {
+
+               addTest(new TestCase("format/toNumber simple") {
+                       @Override
+                       public void test() throws Exception {
+                               assertEquals(263l, StringUtils.toNumber("263"));
+                               assertEquals(21200l, StringUtils.toNumber("21200"));
+                               assertEquals(0l, StringUtils.toNumber("0"));
+                               assertEquals("263", StringUtils.formatNumber(263l));
+                               assertEquals("21k", StringUtils.formatNumber(21000l));
+                               assertEquals("0", StringUtils.formatNumber(0l));
+                       }
+               });
+
+               addTest(new TestCase("format/toNumber not 000") {
+                       @Override
+                       public void test() throws Exception {
+                               assertEquals(263200l, StringUtils.toNumber("263.2k"));
+                               assertEquals(42000l, StringUtils.toNumber("42.0k"));
+                               assertEquals(12000000l, StringUtils.toNumber("12M"));
+                               assertEquals("263k", StringUtils.formatNumber(263012l));
+                               assertEquals("42k", StringUtils.formatNumber(42012l));
+                               assertEquals("12M", StringUtils.formatNumber(12012121l));
+                       }
+               });
+
+               addTest(new TestCase("format/toNumber decimals") {
                        @Override
                        public void test() throws Exception {
-                               assertEquals(StringUtils.toNumber("263"), 263l);
-                               assertEquals(StringUtils.toNumber("21200"), 21200l);
-                               assertEquals(StringUtils.toNumber("0"), 0l);
-                               assertEquals(StringUtils.formatNumber(263l), "263");
-                               assertEquals(StringUtils.formatNumber(21000l), "21k");
-                               assertEquals(StringUtils.formatNumber(0l), "0");
-                               
-                               assertEquals(StringUtils.formatNumber(1287l, false), "1287");
-                               assertEquals(StringUtils.formatNumber(6056l, false), "6k");
-                               
-                               assertEquals(StringUtils.toNumber("263k"), 263000l);
-                               assertEquals(StringUtils.toNumber("42k"), 42000l);
-                               assertEquals(StringUtils.toNumber("12M"), 12000000l);
-                               assertEquals(StringUtils.formatNumber(263012l), "263k");
-                               assertEquals(StringUtils.formatNumber(42012l), "42k");
-                               assertEquals(StringUtils.formatNumber(12012121212l), "12M");
-                               
-                               assertEquals(StringUtils.toNumber("263.2k"), 263200l);
-                               assertEquals(StringUtils.toNumber("1.2k"), 1200l);
-                               assertEquals(StringUtils.toNumber("42.7M"), 42700000000l);
-                               assertEquals(StringUtils.formatNumber(263202l), "263.2k");
-                               assertEquals(StringUtils.formatNumber(1267l), "1.2k");
-                               assertEquals(StringUtils.formatNumber(42712121212l), "42.7M");
+                               assertEquals(263200l, StringUtils.toNumber("263.2k"));
+                               assertEquals(1200l, StringUtils.toNumber("1.2k"));
+                               assertEquals(42700000l, StringUtils.toNumber("42.7M"));
+                               assertEquals(1220l, StringUtils.toNumber("1.22k"));
+                               assertEquals(1432l, StringUtils.toNumber("1.432k"));
+                               assertEquals("1.3k", StringUtils.formatNumber(1300l, 1));
+                               assertEquals("263.2020k", StringUtils.formatNumber(263202l, 4));
+                               assertEquals("1.26k", StringUtils.formatNumber(1267l, 2));
+                               assertEquals("42.7M", StringUtils.formatNumber(42712121l, 1));
                        }
                });
        }
index 02c49959236a83326386a894f7523c0ac751b906..b15fd87ac21e8bfa72c1a89e9f168c16ad9ee9d9 100644 (file)
@@ -22,14 +22,18 @@ public class Test extends TestLauncher {
        public Test(String[] args) {
                super("Nikiroo-utils", args);
 
+               /*
                addSeries(new ProgressTest(args));
                addSeries(new BundleTest(args));
                addSeries(new IOUtilsTest(args));
                addSeries(new VersionTest(args));
                addSeries(new SerialTest(args));
                addSeries(new SerialServerTest(args));
+               */
                addSeries(new StringUtilsTest(args));
+               /*
                addSeries(new TempFilesTest(args));
+               */
                
                // TODO: test cache and downloader
                Cache cache = null;