Re: Formatting a long decimal into Gb, Mb, or Kb (using String.format)
Arvin Portlock wrote:
Eric Sosman wrote:
Um, er, are you aware of the division operator, '/'?
You can use it like this:
if (number > 1000000000)
formatted = Long.toString(number / 1000000000)
+ " GB");
else if (number > 1000000)
Yeah, I feel pretty stupid. Some people ought to have their programming
license revoked:
private String formatNumber (long number) {
String formatted;
if (number > 1000000000) {
formatted = Long.toString (number / 1000000000) + " GB";
} else if (number > 1000000) {
formatted = Long.toString (number / 1000000) + " MB";
} else if (number > 1000) {
formatted = Long.toString (number / 1000) + " KB";
} else {
formatted = Long.toString (number) + " bytes";
}
return (formatted);
}
Incidentally, please use "GB","MB" and not "Gb","Mb".
The lower-case "b" usually signifies bits, not bytes, as
in "It takes ~10 seconds to send 1MB over a 10Mb link."
(However, it must be admitted that computer folk are no
respecters of measurement standards, not even standards
that are the subject of international treaties. For
example, the prefix meaning "kilo" is "k" and not "K",
and it multiplies by 1000, not by 1024.
I didn't know that about big-B vs. little-b. I've changed
that. Thanks for being kind.
There is still a mistake in your implementation. As pointed out by
Eric, "K" means something different than "k".
A base for "K" it is 1024, not 1000!
Consider also using the following approach for your purposes:
public enum StorageUnit {
BYTE ( "B", 1L),
KILOBYTE ("KB", 1L << 10),
MEGABYTE ("MB", 1L << 20),
GIGABYTE ("GB", 1L << 30),
TERABYTE ("TB", 1L << 40),
PETABYTE ("PB", 1L << 50),
EXABYTE ("EB", 1L << 60);
public static final StorageUnit BASE = BYTE;
private final String symbol;
private final long divider; // divider of BASE unit
StorageUnit(String name, long divider) {
this.symbol = name;
this.divider = divider;
}
public static StorageUnit of(final long number) {
final long n = number > 0 ? -number : number;
if (n > -(1L << 10)) {
return BYTE;
} else if (n > -(1L << 20)) {
return KILOBYTE;
} else if (n > -(1L << 30)) {
return MEGABYTE;
} else if (n > -(1L << 40)) {
return GIGABYTE;
} else if (n > -(1L << 50)) {
return TERABYTE;
} else if (n > -(1L << 60)) {
return PETABYTE;
} else { // n >= Long.MIN_VALUE
return EXABYTE;
}
}
public String format(long number) {
return nf.format((double)number / divider) + " " + symbol;
}
private static java.text.NumberFormat nf
= java.text.NumberFormat.getInstance();
static {
nf.setGroupingUsed(false);
nf.setMinimumFractionDigits(0);
nf.setMaximumFractionDigits(1);
}
}
Typical usage is as follows:
StorageUnit.of(number).format(number);
Note that there are still some units of information defined by the SI
not handled here, i.e. bits (and its derived units), zettabytes (ZB),
and yottabytes (YB). Handling them all together requires a bit more
advanced approach (likely something near to the JSR-275 proposals), but
I think you don't need to do that.
piotr