# nikiroo-utils
+## Version 4.6.1
+
+- changed: formatNumber/toNumber and decimals
+
## Version 4.6.0
- new: proxy
* @return the display value
*/
public static String formatNumber(long value) {
- return formatNumber(value, true);
+ return formatNumber(value, 0);
}
/**
* <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;
}
/**
* 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,
* 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,
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) {
}
}
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));
}
});
}
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;