/** Insert commas in the given String, turn "1234" into "1,234". */ public static String commas(String s) { String done = ""; while (s.length() > 3) { // Loop, chopping groups of 3 characters off the end of s done = "," + Text.end(s, 3) + done; s = Text.chop(s, 3); } return s + done; }
/** * Given a number of bytes transferred in a second, describe the speed in kilobytes per second * like "2.24 KB/s". */ public static String speed(int bytesPerSecond) { int i = (bytesPerSecond * 100) / 1024; // Compute the number of hundreadth kilobytes per second if (i == 0) return ""; // Return "" instead of "0.00 KB/s" else if (i < 10) return ("0.0" + i + " KB/s"); // 1 digit "0.09 KB/s" else if (i < 100) return ("0." + i + " KB/s"); // 2 digits "0.99 KB/s" else if (i < 1000) return (Text.start(Number.toString(i), 1) + "." + Text.clip(Number.toString(i), 1, 2) + " KB/s"); // 3 digits "9.99 KB/s" else if (i < 10000) return (Text.start(Number.toString(i), 2) + "." + Text.clip(Number.toString(i), 2, 1) + " KB/s"); // 4 digits "99.9 KB/s" else return commas(Text.chop(Number.toString(i), 2)) + " KB/s"; // 5 or more "999 KB/s" or "1,234 KB/s" }