From: Niki Roo Date: Sat, 13 Apr 2019 06:26:31 +0000 (+0200) Subject: Version 4.6.2 X-Git-Tag: fanfix-3.0.1~29^2~161 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=79961c53a472a86ee151a35149c27cc1617eb779;p=fanfix.git Version 4.6.2 --- diff --git a/VERSION b/VERSION index f4bf7b7..c78c496 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.6.1-dev +4.6.2 diff --git a/changelog.md b/changelog.md index 4c30d16..2472a7f 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index 36fdfbe..1ee9ac4 100644 --- a/src/be/nikiroo/utils/StringUtils.java +++ b/src/be/nikiroo/utils/StringUtils.java @@ -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. *

- * Example: + *

+ * Examples: *

* * @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. *

- * Example: + * Examples (assuming decimalPositions = 1): *

* * @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")) { diff --git a/src/be/nikiroo/utils/test/StringUtilsTest.java b/src/be/nikiroo/utils/test/StringUtilsTest.java index d290ed4..e3be298 100644 --- a/src/be/nikiroo/utils/test/StringUtilsTest.java +++ b/src/be/nikiroo/utils/test/StringUtilsTest.java @@ -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)); } }); }