X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbe%2Fnikiroo%2Futils%2FStringUtils.java;h=954d34113d09650c4b33d5ccd1dbb70f51ed32a1;hb=f743f2ca02a518a7e91ee6376bff28c68977f8da;hp=9acf3a1fd2a72d400168ba74ce515915a7e7fae7;hpb=6003347809dbeb2d8af2fa0ef396baa93a201ec1;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/StringUtils.java b/src/be/nikiroo/utils/StringUtils.java index 9acf3a1..954d341 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: *
- * Example: + * Examples (assuming decimalPositions = 1): *
* 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 +898,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 +912,33 @@ 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); + 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")) { + 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) { } }