From: Niki Roo Date: Fri, 12 Apr 2019 15:41:08 +0000 (+0200) Subject: Version 4.6.1 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=5b46737cf885bd7f5f93bd3a0007795538a8d10d;p=fanfix-jexer.git Version 4.6.1 --- diff --git a/VERSION b/VERSION index 6016e8a..8ac28bf 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.6.0 +4.6.1 diff --git a/changelog.md b/changelog.md index 62e0e65..4c30d16 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # nikiroo-utils +## Version 4.6.1 + +- changed: formatNumber/toNumber and decimals + ## Version 4.6.0 - new: proxy diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index 9acf3a1..2126111 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,44 @@ 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) { + 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. *

* Of course, the conversion to and from display form is lossy (example: - * 6870 to "6k" to 6000). + * 6870 to "6.5k" to 6500). * * @param value * the value in display form with possible "M" and "k" suffixes, @@ -871,7 +885,7 @@ public class StringUtils { * the full value. *

* Of course, the conversion to and from display form is lossy (example: - * 6870 to "6k" to 6000). + * 6870 to "6.5k" to 6500). * * @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) { } } diff --git a/src/be/nikiroo/utils/test/StringUtilsTest.java b/src/be/nikiroo/utils/test/StringUtilsTest.java index 5280181..df2ed5f 100644 --- a/src/be/nikiroo/utils/test/StringUtilsTest.java +++ b/src/be/nikiroo/utils/test/StringUtilsTest.java @@ -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)); } }); } diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index 02c4995..b15fd87 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -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;