Version 4.6.2
authorNiki Roo <niki@nikiroo.be>
Sat, 13 Apr 2019 06:26:31 +0000 (08:26 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 13 Apr 2019 06:26:31 +0000 (08:26 +0200)
VERSION
changelog.md
src/be/nikiroo/utils/StringUtils.java
src/be/nikiroo/utils/test/StringUtilsTest.java

diff --git a/VERSION b/VERSION
index f4bf7b7b39533c077f73eecd437aabdfcad2eb9a..c78c4964cac9470eed7397bb3adc7b45337830fc 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-4.6.1-dev
+4.6.2
index 4c30d16b6a366cd4a0d6b84a4cdded31e73068b4..2472a7f150aaba8454699724bc5a69fdb1bb280b 100644 (file)
@@ -1,8 +1,8 @@
 # nikiroo-utils
 
-## Version 4.6.1
+## Version 4.6.2
 
-- changed: formatNumber/toNumber and decimals
+- fix: formatNumber/toNumber and decimals
 
 ## Version 4.6.0
 
index 36fdfbe2c967359d0677c3935bd1c56b75346b44..1ee9ac4b569001fda8036815fe8a43277e8e18ef 100644 (file)
@@ -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.
         * <p>
-        * Example:
+        * <p>
+        * Examples:
         * <ul>
-        * <li><tt>8765</tt> becomes "8k"</li>
-        * <li><tt>998765</tt> becomes "998k"</li>
-        * <li><tt>12987364</tt> becomes "12M"</li>
+        * <li><tt>8 765</tt> becomes "8k"</li>
+        * <li><tt>998 765</tt> becomes "998k"</li>
+        * <li><tt>12 987 364</tt> becomes "12M"</li>
+        * <li><tt>5 534 333 221</tt> becomes "5G"</li>
         * </ul>
         * 
         * @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.
         * <p>
-        * Example:
+        * Examples (assuming decimalPositions = 1):
         * <ul>
-        * <li><tt>8765</tt> becomes "8.7k"</li>
-        * <li><tt>998765</tt> becomes "998.7k"</li>
-        * <li><tt>12987364</tt> becomes "12.9M"</li>
+        * <li><tt>8 765</tt> becomes "8.7k"</li>
+        * <li><tt>998 765</tt> becomes "998.7k"</li>
+        * <li><tt>12 987 364</tt> becomes "12.9M"</li>
+        * <li><tt>5 534 333 221</tt> becomes "5.5G"</li>
         * </ul>
         * 
         * @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")) {
index d290ed4663dcea44292b19872d4c4f0c76945a26..e3be298f9b897863da57bd20f94f5884c827317a 100644 (file)
@@ -247,6 +247,7 @@ class StringUtilsTest extends TestLauncher {
                                assertEquals(263200l, StringUtils.toNumber("263.2k"));
                                assertEquals(42000l, StringUtils.toNumber("42.0k"));
                                assertEquals(12000000l, StringUtils.toNumber("12M"));
+                               assertEquals(2000000000l, StringUtils.toNumber("2G"));
                                assertEquals("263k", StringUtils.formatNumber(263012l));
                                assertEquals("42k", StringUtils.formatNumber(42012l));
                                assertEquals("12M", StringUtils.formatNumber(12012121l));
@@ -267,7 +268,7 @@ class StringUtilsTest extends TestLauncher {
                                assertEquals("263.2020k", StringUtils.formatNumber(263202l, 4));
                                assertEquals("1.26k", StringUtils.formatNumber(1267l, 2));
                                assertEquals("42.7M", StringUtils.formatNumber(42712121l, 1));
-                               assertEquals("5.09M", StringUtils.formatNumber(5094837485l, 2));
+                               assertEquals("5.09G", StringUtils.formatNumber(5094837485l, 2));
                        }
                });
        }